在学习过程中使用MyEclipse2016,根据学习资料的学习过程发现编写的Servlet在调用是会出问题,经过测试发现MyEclipse2016可一在建servlet类时自动关联web.xml配置文件具体使用方法如下:
- 新建文件选servlet类,输入类名后点击next
选择后系统会自动生成关联,自动生成示文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>Myservlet</servlet-name>
<servlet-class>servlet1.Myservlet</servlet-class>
</servlet>
<servlet>
<servlet-name>testServlet</servlet-name>
<servlet-class>servlet1.testServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Myservlet</servlet-name>
<url-pattern>/servlet/Myservlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>testServlet</servlet-name>
<url-pattern>/testServlet</url-pattern>
</servlet-mapping>
</web-app>
编写测试Servlet类如下:
测试代码
package servlet1;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Myservlet extends HttpServlet {
public Myservlet() {
super();
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
response.getWriter().write("now time:" + new Date());
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
使用刚才配置文件配置方式