Simple usage and configuration examples and descriptions of servlet



The explanation of servlet in Baidu is:

A server-side program written in Java. Its main function is to browse and modify data interactively and generate dynamic Web content. A Servlet in a narrow sense refers to an interface implemented by the Java language, and a Servlet in a broad sense refers to any class that implements the Servlet interface. In general, people understand Servlet as the latter.


Learn and understand:

As far as I know, there are many ways to write servlets, and it is more common to integrate the httpservlet class, and then override methods such as doget and dopost. So I base my understanding on this.

This simple implementation needs to import the jar package of the servlet in the project.

To verify that the servlet you wrote and the configuration are correct, you need to involve a simple page, jsp or html, as follows:

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.   <metahttp-equiv="Content-Type"content="text/html; charset=utf-8"/>     
  5. </head>  
  6. <body>  
  7.   <formaction="./cookieServlet"method="post">    
  8.      <inputtype="text"name="userName"/>    
  9.      <inputtype="password"name="password"/>    
  10.      <inputtype="submit"value="login"/>    
  11.   </form>  
  12. </body>  
  13. </html>  
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
  <form action="./cookieServlet" method="post">
     <input type="text" name="userName"/>
     <input type="password" name="password"/>
     <input type="submit" value="login"/>
  </form>
</body>
</html>

The page is just a very simple form, containing two text input boxes for username and password, and a form submit button. It should be noted that the name attribute cannot be missing from the input. This attribute is the key to interacting with the background. With it, the background can use the request.getParameter() method to correctly obtain the content entered in the text box. Experienced friends must know this, but when I first entered software development, I encountered difficulties because I did not know this problem. I think it is probably useful for beginners.

In this page, after filling in the user name and password, click submit, the browser will send a cookieServlet request to the server, and at the same time pass the userName and password filled in the page into the background. The background can obtain data through corresponding methods and perform certain logical processing. The corresponding servlet code is as follows:

  1. package servletTest;  
  2.   
  3. import java.io.IOException;  
  4. import javax.servlet.ServletException;  
  5. import javax.servlet.http.Cookie;  
  6. import javax.servlet.http.HttpServlet;  
  7. import javax.servlet.http.HttpServletRequest;  
  8. import javax.servlet.http.HttpServletResponse;  
  9. import javax.servlet.http.HttpSession;  
  10.   
  11. public class CookieServlet extends HttpServlet {  
  12.   
  13.     @Override  
  14.     protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
  15.             throws ServletException, IOException {  
  16.   
  17.         super.doGet(req, resp);  
  18.     }  
  19.   
  20.     @Override  
  21.     protected void doPost(HttpServletRequest req, HttpServletResponse resp)  
  22.             throws ServletException, IOException {  
  23.         String userName = req.getParameter("userName");  
  24.         String password = req.getParameter("password");  
  25.     }  
  26. }  
package servletTest;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class CookieServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String userName = req.getParameter("userName");
        String password = req.getParameter("password");
    }
}

在这个代码中,自己新建了一个CookieServlet类,集成了httpservlet并重写doPost方法,在方法里获取userName和password。

截止到这里,看起来都非常的简单,不论是页面还是java代码都不难理解。但是如果只是这样的话,实际上是不行的,还需要配置web.xml文件才能让前后台联系起来,成功的实现交互。web.xml如下:
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  5.     id="WebApp_ID" version="2.5">      
  6.   
  7.   <display-name>cookieTest</display-name>  
  8.   <servlet>  
  9.      <servlet-name>login</servlet-name>  
  10.      <servlet-class>servletTest.CookieServlet</servlet-class>  
  11.   </servlet>  
  12.   
  13.   <servlet-mapping>  
  14.      <servlet-name>login</servlet-name>  
  15.      <url-pattern>/cookieServlet</url-pattern>  
  16.   </servlet-mapping>  
  17.   
  18.   <welcome-file-list>  
  19.     <welcome-file>index.html</welcome-file>  
  20.   </welcome-file-list>  
  21. </web-app>  
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">    

  <display-name>cookieTest</display-name>
  <servlet>
     <servlet-name>login</servlet-name>
     <servlet-class>servletTest.CookieServlet</servlet-class>
  </servlet>

  <servlet-mapping>
     <servlet-name>login</servlet-name>
     <url-pattern>/cookieServlet</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>

这里边,首先要注册一个servlet,也就是下边这个:
<servlet>
     <servlet-name>login</servlet-name>
     <servlet-class>servletTest.CookieServlet</servlet-class>
  </servlet>

然后是对应的映射:
<servlet-mapping>
     <servlet-name>login</servlet-name>
     <url-pattern>/cookieServlet</url-pattern>
  </servlet-mapping>

那么有了这两个配置后,当页面向服务器发送cookieServlet请求后,web.xml就会找到 url-pattern>/cookieServlet</url-pattern>这里,然后根据这个找个他所在的<servlet-mapping>里的<servlet-name>是什么。

It can be seen that in my case, the corresponding <servlet-name> is login, so web.xml is looking for <servlet> containing <servlet-name>login</servlet-name> according to this login .

When you find this <servlet>, you can get the <servlet-class>servletTest.CookieServlet</servlet-class>, where the full path of the background servlet class we wrote is written in the <servlet-class>. It is the full package name plus the class name.

So, with this configuration, our program knows which background code the corresponding request should go to, so as to achieve association and interaction.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326116105&siteId=291194637