servlet 1

servlet part 1

1. Handling form login:

  1. Write a landing page first:

    

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="UTF-8">
 5 <title>login</title>
 6 </head>
 7 <body>
 8     <form action="/test/login" method="post">
 9         <table>
10             <tr>
11                 <td>name</td>
12                 <td><input type="text" name="userid"></td>
13             </tr>
14             <tr>
15                 <td>password</td>
16                 <td><input type="password" name="psw" /></td>
17             </tr>
18             <tr>
19                 <td><input value="submit" type="submit"></td>
20                 <td><input value="reset" type="reset"></td>
21             </tr>
22         </table>
23     </form>
24 </body>
25 </html>
View Code

  2. Start the servlet to process the form:

    Note: First, you need to put the jar package of the servlet api into the lib directory under wed-info of webcontent.

    The overall structure of the project:

      

    Then configure the web.xml file,:

      

1  // This tag configures the default page, you can refer to another article 
2 <welcome-file-list>
 3      <welcome-file>hello.html</welcome-file>
 4    </welcome-file-list>
 5  // Give The name of your servlet class, the general name is the same as the class name, the following is your class plus the package name 
6    <servlet>
 7      <servlet-name>LoginServlet</servlet-name>
 8      <servlet- class >com.yang .LoginServlet</servlet- class >
 9    </servlet>
 10  // This is an alias for your application, which can be accessed in the browser, here is login 
11    <servlet-mapping>
 12      <servlet-name >LoginServlet</servlet-name>
 13     <url-pattern>/login</url-pattern>
14   </servlet-mapping>
View Code

 

2. The servlet adds, deletes and changes the database:

  Check

 1 package com.yang;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 import java.sql.DriverManager;
 6 import java.sql.SQLException;
 7 import java.sql.Connection;
 8 import java.sql.*;
 9 import javax.servlet.ServletException;
10 import javax.servlet.annotation.WebServlet;
11 import javax.servlet.http.HttpServlet;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14 /**
15  * Servlet implementation class Chakan
16  */
17 @WebServlet("/Chakan")
18 public class Chakan extends HttpServlet {
19     private static final long serialVersionUID = 1L;
20     private static String CONTENT_TYPE="text/html;charset=GBK";   
21     /**
22      * @see HttpServlet#HttpServlet()
23      */
24     public Chakan() {
25         super();
26         // TODO Auto-generated constructor stub
27     }
28     
29     public void init(){
30         
31     }
32 
33     /**
34      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
35      */
36     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
37         // TODO Auto-generated method stub
38 //        System.err.println(response.getWriter().append("Served at: ").append(request.getContextPath()));
39         response.setContentType(CONTENT_TYPE);
40         PrintWriter out=response.getWriter();
41         request.setCharacterEncoding("GBK");
42         response.setCharacterEncoding("GBK");
43         //连接数据库
44         try {
45             Class.forName("com.mysql.jdbc.Driver");
46             String url="jdbc:mysql://localhost:3306/stu";
47             String user="yang";
48             String psw="19970817";
49             Connection con=DriverManager.getConnection(url, user, psw);
50             Statement rs=con.createStatement();
51             ResultSet res=rs.executeQuery("select * from student");
52             out.println("<html>");
53             out.print("<head><title>Chakan</title></head>");
54             out.println("<body>");
55             out.println("<table align='center' border='1px' cellpadding='0' colspadding='0'><tr><td>id</td><td>姓名</td><td>班级</td><td>修改</td><td>删除</td></tr>");
56             while(res.next()){
57                 out.println("<tr><td>"+res.getString("id")+"</td><td>"+res.getString("name")+"</td><td>"+res.getShort("classgrent")+
58                         "</td><td><a href='update?id="+res.getString("id")+"'>修改</a></td><td><a href='delete?id="+res.getString("id")+
59                         "'>删除</a></td></tr>");
60             }
61             out.println("</table></body></html>");
62             out.flush();
63             res.close();
64             con.close();
65         } catch (ClassNotFoundException | SQLException e) {
66             // TODO Auto-generated catch block
67             e.printStackTrace();
68         }
69         out.close();
70     }
71 
72     /**
73      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
74      */
75     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
76         // TODO Auto-generated method stub
77         doGet(request, response);
78     }
79 
80 }
View Code

 

 

3. Working principle of servlet:

  

  

4. The servlet inherits the httpServlet class:

  (1) dopost() method:

      Process the post submission.

   (2) doget() method:  

      Process the get submission.

5. How to publish the servlet project to Tomcat:

  

 

 

 

 

 

Guess you like

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