Home  >  Article  >  Java  >  How to configure xml for Servlet

How to configure xml for Servlet

(*-*)浩
(*-*)浩Original
2019-05-06 13:44:519897browse

This article will briefly introduce how servlet performs simple configuration of xml files. If you want to learn more, recommend the course: Java Tutorial.

How to configure xml for Servlet

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 ##name>simple, and then locate the servlet file: cn .mldn.lxh.servlet.SimpleServlet

It can be seen that the name simple in simple does not necessarily have to be consistent with the servlet file name "SimpleServlet.java" , it is just the definition of the servlet application name when configuring the web.xml file. We can locate this servlet file through cn.mldn.lxh.servlet.SimpleServlet .

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What is ServletContextNext article:What is ServletContext