This article will briefly introduce how servlet performs simple configuration of xml files. If you want to learn more, recommend the course: Java Tutorial.
Write a class in the web project.
File name: "SimpleServlet.java"
package cn.mldn.lxh.servlet ;//定义包 import java.io.* ; // HttpServlet属于javax.servlet.http包下 // ServletException属于javax.servlet包下 import javax.servlet.* ;//导入HttpServlet所属的包 // HttpServletRequest、HttpServletResponse存放在javax.servlet.http包下 import javax.servlet.http.* ; public class SimpleServlet extends HttpServlet { // 表示处理get请求 public void doGet(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException //抛出异常 { PrintWriter out = resp.getWriter() ;//实例化out对象。 out.println("<HTML>") ; out.println("<HEAD>") ; out.println("<TITLE>THE FIRST SERVLET</TITLE>") ; out.println("</HEAD>") ; out.println("<BODY>") ; out.println("<H1>Hello World!!!</H1>") ; out.println("</BODY>") ; out.println("</HTML>") ; out.close() ; } public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException { this.doGet(request,response) ; } };
JSP function is consistent with servlet, which means that servlet can be accessed externally, so to access it you need to pass an address, so it can only be accessed through WEB The address mapping is solved.
How to perform address mapping?
At this time, we need to configure the web.xml
file to change it to the address and path we want.
<servlet> <servlet-name>simple</servlet-name>//我们定义的servlet应用名字 <servlet-class>cn.mldn.lxh.servlet.SimpleServlet</servlet-class> //我们定义的servlet应用名字对应的具体servlet文件 </servlet> <servlet-mapping> //地址映射 <servlet-name>simple</servlet-name> //我们定义的servlet应用名字 <url-pattern>/demo</url-pattern> //地址名 </servlet-mapping>
The purpose of row address mapping is to use servlet. Its working process is:
Working process:
Input address: http:// localhost:8080/ demo, use it to find the file name simple inside the mapping file, use simple to find the corresponding
The above is the detailed content of How to configure xml for Servlet. For more information, please follow other related articles on the PHP Chinese website!