mvc架构的简单登录系统,jsp

文件结构

三个jsp文件负责前段界面的实现

login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <head>  
    <title>My JSP 'login.jsp' starting page</title>
    <script>
            function myFunction(){
               if(myForm.username.value==""){
                  alert("用户名不能为空");
               }
               else if(myForm.password.value==""){
                  alert("密码不能为空");
               }
               else{
                  myForm.submit();
               }
            }
    </script>
  </head>
  <body>
  <form action="LoginServlet" method="post" name="myForm"> 
  <center>
  <table>
     <tr>
        <td>用户名</td>
        <td><input type="text" name="username"></td>
        <td>用户名不能为空</td>
     </tr>
     <tr>
        <td>密码</td>
        <td><input type="password" name="password"></td>
        <td>密码不能为空</td>
     </tr>
     <tr>
        <td></td>
        <td><input type="submit" value="提交" onclick="myFunction()">
            <input type="reset" value="取消"></td>
     </tr>
  </table>
  </center>
  </form>
  </body>
</html>

success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <head>  
    <title>My JSP 'success.jsp' starting page</title>
  </head>  
  <body>
    <%
       out.println("登录成功!");
     %>
  </body>
</html>
View Code

error.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <head>  
    <title>My JSP 'error.jsp' starting page</title>
  </head>
  <body>
    <%
       out.println("用户名或密码错误,登录失败!");
     %>
  </body>
</html>

javabean文件

JdbcBean.java

package data;
import java.sql.*;
public class JdbcBean {
      private String DBDriver="com.mysql.jdbc.Driver";
      private String DBUrl="jdbc:mysql://localhost:3306/trade";
      private String DBUser="root";
      private String DBPassword="admin";
      public Connection conn=null;
      public ResultSet rs=null;
      public ResultSet query(String sql)throws Exception{
          try{
              Class.forName(DBDriver);
              conn=DriverManager.getConnection(DBUrl,DBUser,DBPassword);
              Statement stmt=conn.createStatement();
              rs=stmt.executeQuery(sql);
              return rs;
          }
          catch(SQLException e){
              System.out.println(e.getMessage());
          }
          return null;
      }
      public void update(String sql)throws Exception{
          try{
              Class.forName(DBDriver);
              conn=DriverManager.getConnection(DBUrl,DBUser,DBPassword);
              Statement stmt=conn.createStatement();
              stmt.executeUpdate(sql);
              stmt.close();
              conn.close();
          }
          catch(SQLException e){
              System.out.println(e.getMessage());
          }
      }
}
View Code

servlet文件

LoginServlet.java

package data;

import java.io.IOException;
import java.sql.ResultSet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet {
    public void doPost(HttpServletRequest request,
                       HttpServletResponse response)
            throws ServletException, IOException {
        String toJSP="";
        request.setCharacterEncoding("gbk");
        response.setContentType("text/html;charset=gbk");
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String sql="select * from userChart where userId='"+username+"'" +
                   "&&userPassword='"+password+"'";
        JdbcBean jdbc=new JdbcBean();
        try{
            ResultSet rs=jdbc.query(sql);
            if(rs.next()){
                toJSP="success.jsp";
            }
            else{
                toJSP="error.jsp";
            }
            rs.close();
        }
        catch(Exception e){
            System.out.println("������ѯ�쳣");
        }
        RequestDispatcher rd=request.getRequestDispatcher(toJSP);
        rd.forward(request, response);
    } 
    public void doGet(HttpServletRequest request, 
                      HttpServletResponse response)
      throws ServletException, IOException {
            doPost(request,response);
    }
}
View Code

数据库为mysql,用户名:root;密码:admin。数据库名:trade;表名:userchart

参考:http://blog.csdn.net/lindonglian/article/details/40581117?utm_source=tuicool&utm_medium=referral

http://www.xin126.cn/show.asp?id=2504

posted @ 2016-06-02 17:02  JarvisLau  阅读(3343)  评论(0编辑  收藏  举报