zoukankan      html  css  js  c++  java
  • servlet之注册登录(简写)

    1.注册页面

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 <script type="text/javascript" src="../js/jquery-1.6.4.min.js"></script>
     9 <script type="text/javascript" src="../js/jquery.validate.js"></script>
    10 <script type="text/javascript">
    11    $(function()
    12            {
    13              $("form").validate();
    14        
    15            })
    16 </script>
    17 </head>
    18 <body>
    19 <form action="../Servlet" method="post">
    20 用户名:<input type="text" name="userName" required /><br/>
    21 密码:<input type="password" name="password" id="l1" maxlength="10" minlength="6" required /><br/>
    22 确认密码:<input type="password" name="password" equalTo="#l1" required /><br/>
    23 性别:<input type="radio" name="sex" required />24 <input type="radio" name="sex"/>女<br/>
    25 出生日期:<input type="date" name="birthday" required /><br/>
    26 个人爱好:<input type="checkbox" name="hobby" required />游泳
    27 <input type="checkbox" name="hobby"/>篮球
    28 <input type="checkbox" name="hobby"/>排球
    29 <input type="checkbox" name="hobby"/>气球<br/>
    30 <button type="submit">注册</button>
    31 <button type="reset">重置</button>
    32 </form>
    33 </body>
    34 </html>
    View Code

    2.servlet

     1 package com.zdsofe.servlet1;
     2 
     3 import java.io.IOException;
     4 import java.sql.Connection;
     5 import java.sql.DriverManager;
     6 import java.sql.SQLException;
     7 import java.sql.Statement;
     8 
     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 
    16 
    17 /**
    18  * Servlet implementation class Servlet
    19  */
    20 @WebServlet("/Servlet")
    21 public class Servlet extends HttpServlet {
    22     private static final long serialVersionUID = 1L;
    23     private static String DRIVER="com.mysql.jdbc.Driver";
    24     private static String URL="jdbc:mysql://localhost:3306/mysql";
    25     private static String user="root";
    26     private static String key="775297";
    27      Connection conn;
    28      int result=0;
    29     //加载驱动
    30     static{
    31         try {
    32             Class.forName(DRIVER);
    33         } catch (ClassNotFoundException e) {
    34             e.printStackTrace();
    35         }
    36     }
    37     /**
    38      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    39      */
    40     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    41         /*request.getRequestDispatcher("https://www.baidu.com").forward(request, response);*/        
    42         response.sendRedirect("/webProject1/Pages/Welcome.jsp");
    43               
    44     }
    45 
    46     /**
    47      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    48      */
    49     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    50                 //设置请求字符编码
    51                 request.setCharacterEncoding("utf-8");
    52                 //设置响应字符编码
    53                 response.setCharacterEncoding("utf-8");
    54                 response.setContentType("text/html;charset=utf-8");
    55                 //获取用户名
    56                 String userName=request.getParameter("userName");
    57                 //密码
    58                 String password=request.getParameter("password");
    59                 try {
    60                      //连接数据库
    61                      conn = DriverManager.getConnection(URL,user,key);
    62                      //创建SQL语句对象
    63                      Statement stmt=conn.createStatement();
    64                      String sql="insert into denglu(userName,mima) values('"+userName+"','"+password+"')";
    65                       result= stmt.executeUpdate(sql);    
    66                 } catch (SQLException e) {
    67                     e.printStackTrace();
    68                 }
    69                 if(result==1)
    70                 {
    71                     //response.sendRedirect(request.getContextPath()+"/jsp-login/welcome.jsp");
    72                     //request.getRequestDispatcher("/Pages/DengLu.jsp?userN="+userName).forward(request, response);
    73                     request.getRequestDispatcher("/Pages/DengLu.jsp").forward(request, response);
    74                 }
    75                 else
    76                 {
    77                     request.setAttribute("error", "注册失败!");
    78                     request.getRequestDispatcher("/Pages/ZhuCe.jsp").forward(request, response);
    79                 }
    80     }
    81 
    82 }
    View Code

    3.登录端

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10 <form action="Servlet" method="get">
    11 用户名:<input type="text" name="userName1"/><br/>
    12 密码:<input type="password" name="password1"/><br/>
    13 <button type="submit">登录</button>
    14 <button type="reset">重置</button>
    15 </form>
    16 </body>
    17 </html>
    View Code

    4.欢迎页面

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10 welcome!
    11 </body>
    12 </html>
    View Code
  • 相关阅读:
    怎样不重启设置字体边缘平滑立即生效! 以下注册表导入后不能立即生效。。
    Delphi下实现全屏快速找图找色 一、数据提取
    delphi2006语言新特性:Record类型高级用法
    delphi之模糊找图
    delphi之精确找图
    delphi2006语言新特性——类静态字段、类属性
    Delphi程序开启XP的ClearType显示效果
    delphi Createthread的线程传参数
    修改窗体非客户区大小更改窗体标题栏高度
    windows 匿名管道读取子进程输出
  • 原文地址:https://www.cnblogs.com/zclqian/p/7232177.html
Copyright © 2011-2022 走看看