Servlet用户管理系统继续开发

一、增加到数据库中查询验证:

1.创建数据库user

Servlet用户管理系统继续开发
create table user(
id int primary key,
username varchar(32) not null,
email varchar(64),
grade int default 1,
password varchar(32) not null
)
View Code

 利用数据库进行验证

ControlServlet.java

Servlet用户管理系统继续开发
package com.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.Response;
import java.sql.*;
import com.test.TestServlet;

/**
 * Servlet implementation class ControlServlet
 */
@WebServlet("/ControlServlet")
public class ControlServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public ControlServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        String id = request.getParameter("id");
        String password = request.getParameter("password");
        // 到数据库中验证
            Connection conn = null;
            PreparedStatement ps = null;
            Statement stmt = null;
            ResultSet rs = null;
            try {// 加载驱动
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                System.out.println("建立连接失败");
            }

            try {// 创建连接对象
                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/user", "root", "123456");
                stmt = conn.createStatement();// 创建sql操作对象
                ps=conn.prepareStatement("select * from user where id=?and password=?");
                //赋值
                ps.setObject(1,id);
                ps.setObject(2, password);
rs=ps.executeQuery();
               if(rs.next()){
                  //该用户合法
                   response.sendRedirect("/user/MainFrame");
               }else{
                   response.sendRedirect("/user/Loin");
               }
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("建立连接失败");
            } finally {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }

            }

    
        
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        this.doGet(request, response);
    }

}
View Code

LgionServlet.java

Servlet用户管理系统继续开发
package com.view;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Loin
 */
@WebServlet("/Loin")
public class LoinServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoinServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //浏览器uft-8展示
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        PrintWriter out=response.getWriter();
        //返回一个界面
//        out.println("你好");
        out.println("<h1>用户登录</h1>");
        out.println("<form action='/user/ControlServlet' method='post'>");
        out.println("id:<input type='text' name='id'></br>");
        out.println("密 码:<input type='password' name='password'></br>");
        out.println("<input type='submit' value='登录'></br>");
        out.println("</form>");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        this.doGet(request,response);
    }

}
View Code

MainFrame.java

Servlet用户管理系统继续开发
package com.view;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.test.TestServlet;

/**
 * Servlet implementation class MainFrame
 */
@WebServlet("/MainFrame")
public class MainFrame extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public MainFrame() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        String username=request.getParameter("uname");
        String username2=(String) request.getSession().getAttribute("loginuser");
        //获取Session中的对象
        TestServlet user=(TestServlet) request.getSession().getAttribute("userobj");
        out.println("<h1>登陆成功</h1>");
        out.println("<a href='/user/Loin'>返回重新登陆</a>");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        this.doGet(request, response);
    }

}
View Code

 

上一篇:Servlet简单例子:模拟登入


下一篇:doGet()、doPost() 、service()之间关系