码迷,mamicode.com
首页 > 其他好文 > 详细

servlet 1

时间:2018-05-05 20:43:12      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:next   bubuko   package   att   ext   bcb   api   delete   result   

servlet 第一篇

一、处理表单登录:

  1、先写一个登陆页面:

    

技术分享图片
 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、开始servlet处理表单:

    注:首先需要将servlet的api的jar包放入到webcontent的wed-info下的lib目录。

    项目整体结构:

      技术分享图片

    然后配置web.xml文件,:

      

技术分享图片
 1 //这个标签配置默认页面,可以参考另一篇
 2 <welcome-file-list>
 3     <welcome-file>hello.html</welcome-file>
 4   </welcome-file-list>
 5 //给你的servlet类命名,一般名字取与类相同的名字,下面是你的类加上包名
 6   <servlet>
 7     <servlet-name>LoginServlet</servlet-name>
 8     <servlet-class>com.yang.LoginServlet</servlet-class>
 9   </servlet>
10 //此处是给你的应用起别名,能在浏览器中访问,此处起的是login
11   <servlet-mapping>
12     <servlet-name>LoginServlet</servlet-name>
13     <url-pattern>/login</url-pattern>
14   </servlet-mapping>
View Code

 

2、servlet对于数据库的增删改差:

  查看

技术分享图片
 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、servlet工作原理:

  技术分享图片

  技术分享图片

4、servlet继承httpServlet类:

  (1)dopost()  方法:

      对post提交进行处理。

   (2)doget() 方法:  

      对get提交进行处理。

5、如何将servlet项目发布到Tomcat:

  

 

 

 

 

 

servlet 1

标签:next   bubuko   package   att   ext   bcb   api   delete   result   

原文地址:https://www.cnblogs.com/yangsongwei/p/8995654.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!