Servlet<4>
来源:互联网 发布:谜一样的双眼知乎 编辑:程序博客网 时间:2024/06/16 03:41
下午弄了一下午这个登录 蛋疼的要死= =不过最好还是成功了 = =
用的是mysql的数据库 账号root 密码root
建db_jsp userName password
model
package com.ruanku.model;public class User {private int id;private String userName;private String password;public User() {super();// TODO Auto-generated constructor stub}public User(String userName, String password) {super();this.userName = userName;this.password = password;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}dao
package com.ruanku.dao;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import com.ruanku.model.User;public class UserDao {public User login(Connection con,User user)throws Exception{User resultUser=null;String sql="select * from t_user where userName=? and password=?";PreparedStatement pstmt=con.prepareStatement(sql);pstmt.setString(1, user.getUserName());pstmt.setString(2, user.getPassword());ResultSet rs=pstmt.executeQuery();if(rs.next()){resultUser=new User();resultUser.setUserName(rs.getString("userName"));resultUser.setPassword(rs.getString("password"));}return resultUser;}}
util
package com.ruanku.util;import java.sql.Connection;import java.sql.DriverManager;public class DbUtil {private String dbUrl="jdbc:mysql://localhost:3306/db_jsp";private String dbUserName="root";private String dbPassword="root";private String jdbcName="com.mysql.jdbc.Driver";public Connection getCon()throws Exception{Class.forName(jdbcName);Connection con=DriverManager.getConnection(dbUrl, dbUserName, dbPassword);return con;}public void closeCon(Connection con)throws Exception{if(con!=null){con.close();}}public static void main(String[] args) {DbUtil dbUtil=new DbUtil();try {dbUtil.getCon();System.out.println("连接成功");} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
web
package com.ruanku.web;import java.io.IOException;import java.sql.Connection;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import com.ruanku.dao.UserDao;import com.ruanku.model.User;import com.ruanku.util.DbUtil;public class LoginServlet extends HttpServlet{/** * */private static final long serialVersionUID = 1L;DbUtil dbUtil=new DbUtil();UserDao userDao=new UserDao();@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {this.doPost(request, response);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {String userName=request.getParameter("userName");String password=request.getParameter("password");Connection con=null;try {User user=new User(userName,password);con=dbUtil.getCon();User currentUser=userDao.login(con, user);if(currentUser==null){request.setAttribute("error", "用户名或密码错误");request.setAttribute("userName", userName);request.setAttribute("password", password);request.getRequestDispatcher("login.jsp").forward(request, response);}else{HttpSession session=request.getSession();session.setAttribute("currentUser", currentUser);response.sendRedirect("main.jsp");}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
xml
<servlet><servlet-name>loginServlet</servlet-name><servlet-class>com.ruanku.web.LoginServlet</servlet-class></servlet><servlet-mapping><servlet-name>loginServlet</servlet-name><url-pattern>/login</url-pattern></servlet-mapping>
jsp文件
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><form action="login" method="post"><table><tr><th colspan="2">用户登录</th></tr><tr><td>用户名:</td><td><input type="text" id="userName" name="userName" value="${userName }"/></td></tr><tr><td>密码:</td><td><input type="password" id="password" name="password" value="${password }"/></td></tr><tr><td><input type="submit" value="登录"/></td><td><font color="red">${error }</font></td></tr></table></form></body></html>
main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body>主页!当前登录用户:${currentUser.userName }</body></html>
0 0
- Servlet(4)
- servlet(4)
- Servlet 4
- Servlet<4>
- Servlet-4
- Servlet/JSP学习笔记(4)-Servlet入门
- Servlet/JSP学习笔记(4)-Servlet入门
- java servlet帮助4
- Servlet 4 过滤器
- servlet 与缓存(4)
- servlet开发4-HttpServlet
- servlet-4 状态码
- Servlet应用程序(4)----数据库
- Servlet基础(4)
- jsp+servlet(4)
- Servlet
- servlet
- Servlet
- 《unix高级环境编程》高级 I/O——STREAMS(流)
- 关 于 PV 操 作
- rabbitmq 安装负载均衡配置
- 二讲:android 是什么?android 现有成员有哪些?
- IO流 System.in BufferedReader的readLine()方法 toUpperCase()方法 equalsIgnoreCase("exit")
- Servlet<4>
- 《unix高级环境编程》高级 I/O—— I/O 多路转接
- Ubuntu系统下Hadoop 2.0.4集群安装配置
- 中科院博士教你如何查找外文文献
- 第十三周项目六:体验文件操作3.0
- 《unix高级环境编程》高级 I/O—— readv 和 writev 函数
- 【计算几何初步-线段相交】【HDU1089】线段交点
- MediaPlayer和AudioTrack播放Audio的区别与联系
- 新旧代码兼容