为什么要配置web.xml
就是为自己创建的一个servlet项目设置一个URL。
配置如下
代码如下
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>Servlet01_response</servlet-name>
<servlet-class>response.Servlet01_response</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet01_response</servlet-name>
<url-pattern>/org.cn</url-pattern>
</servlet-mapping>
</web-app>
展示
代码如下:
package response;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/Servlet01_response")
public class Servlet01_response extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");//解决乱码问题
PrintWriter writer=response.getWriter();
writer.write("好好学习,天天向上!");
}
}
注意的是:
该代码的URL为:/Servlet01_response
使用原来的URL
使用自己配置的