package Bean;
public class login {
private String user;
private String password;
public login(){
super();
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}package Dao;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import Bean.login;import Util.DBUtil;public class loginDao { public static Bean.login login(String user) throws SQLException{ //static Bean.login根据提示修改 String sql = "select * from user where user_name ='" +user+"'"; Connection con = DBUtil.getConnection(); Statement stmd = con.createStatement(); ResultSet rs = stmd.executeQuery(sql); while(rs.next()){ login u = new login(); u.setUser(rs.getString("user")); u.setPassword(rs.getString("password")); return null; } return null; } }package Servlet;import java.io.IOException;import java.io.PrintWriter;import java.sql.SQLException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.swing.JOptionPane;import Bean.login;import Dao.loginDao;public class loginServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 6835706189070152812L; /** * Constructor of the object. */ public loginServlet() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { return; } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String user = request.getParameter("user"); String password = request.getParameter("password"); response.setContentType("text/html"); //PrintWriter out = response.getWriter(); try{ login lg = new Bean.login(); lg = loginDao.login("user"); if(user != null){ if(lg.getPassword().endsWith(password)){ request.getRequestDispatcher("index.jsp") .forward(request, response); }else{ request.getRequestDispatcher("login.jsp") .forward(request, response); } }else{ JOptionPane.showMessageDialog(null, "用户名不存在!"); response.sendRedirect("Login.jsp"); } }catch(SQLException e){ e.printStackTrace(); } } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here }}package Util;import java.sql.*;public class DBUtil { public static Connection getConnection() throws SQLException{ String url = "jdbc:mysql://192.168.100.1:3306/bus"; String user = "root"; String password = ""; Connection con = null; try{ Class.forName("com.mysql.jdbc.Driver"); con=DriverManager.getConnection(url,user,password); }catch(Exception e){ e.printStackTrace(); } return con; } }<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <title>登录</title> <body> <form action="servlet/loginServlet" method="post"> 用户名:<input type="text" name="user"> 密码: <input type="text" name="password"> <input type="submit" name="login" value="submit"> </form> </body></html>
添加回答
举报
0/150
提交
取消