要进行购买,还需要用户处于登陆状态,因为后续生成的订单,需要挂在某个用户的名下。
步骤1:SQL
步骤2:User
步骤3:UserDAO
步骤4:login.jsp
步骤5:UserLoginServlet
步骤6:配置web.xml
步骤7:listProduct.jsp
步骤8:测试
步骤 1 : SQL
与Product模块类似的,不提供完整的用户CRUD(增 删 改 查) 也是通过SQL准备User数据。
create table user(
id int,
name varchar(50),
password varchar(50)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
insert into user values(1,'tom','123');
|
步骤 2 : User
User 类
package bean;
public class User {
private int id;
private String name;
private String password;
public int getId() {
return id;
}
public void setId( int id) {
this .id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this .password = password;
}
}
|
步骤 3 : UserDAO
根据name和password查询表user,如果有数据就表示账号密码正确
package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import bean.User;
public class UserDAO {
public static void main(String[] args) {
System.out.println( new UserDAO().getUser( "tom" , "123" ).getId());
}
public User getUser(String name, String password) {
User result = null ;
try {
Class.forName( "com.mysql.jdbc.Driver" );
Connection c = DriverManager.getConnection( "jdbc:mysql://127.0.0.1:3306/cart?characterEncoding=UTF-8" ,
"root" , "admin" );
String sql = "select * from user where name = ? and password = ?" ;
PreparedStatement ps = c.prepareStatement(sql);
ps.setString( 1 , name);
ps.setString( 2 , password);
ResultSet rs = ps.executeQuery();
if (rs.next()){
result = new User();
result.setId(rs.getInt( 1 ));
result.setPassword(password);
result.setName(name);
}
ps.close();
c.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}
|
步骤 4 : login.jsp
登陆页面
<%@ page language= "java" contentType= "text/html; charset=UTF-8"
pageEncoding= "UTF-8" import = "java.util.*" %>
<!DOCTYPE html>
<meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" >
<form action= "login" method= "post" >
账号: <input type= "text" name= "name" > <br>
密码: <input type= "password" name= "password" > <br>
<input type= "submit" value= "登录" >
</form>
|
步骤 5 : UserLoginServlet
登陆Servlet, 通过name和password获取user对象
如果对象不为空,就表示账号密码正确,跳转到产品显示界面 /listProduct
如果对象为空,就跳转到登陆界面,重新登陆
package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import bean.User;
import dao.UserDAO;
public class UserLoginServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter( "name" );
String password = request.getParameter( "password" );
User user = new UserDAO().getUser(name, password);
if ( null != user) {
request.getSession().setAttribute( "user" , user);
response.sendRedirect( "/listProduct" );
} else
response.sendRedirect( "/login.jsp" );
}
}
|
步骤 6 : 配置web.xml
在web.xml中为路径/login加上相关配置
<? xml version = "1.0" encoding = "UTF-8" ?>
< web-app >
< servlet >
< servlet-name >ProductListServlet</ servlet-name >
< servlet-class >servlet.ProductListServlet</ servlet-class >
</ servlet >
< servlet-mapping >
< servlet-name >ProductListServlet</ servlet-name >
< url-pattern >/listProduct</ url-pattern >
</ servlet-mapping >
< servlet >
< servlet-name >LoginServlet</ servlet-name >
< servlet-class >servlet.UserLoginServlet</ servlet-class >
</ servlet >
< servlet-mapping >
< servlet-name >LoginServlet</ servlet-name >
< url-pattern >/login</ url-pattern >
</ servlet-mapping >
</ web-app >
|
步骤 7 : listProduct.jsp
修改listProduct.jsp,如果用户登陆了,就显示用户的名字
<c: if test= "${!empty user}" >
<div align= "center" >
当前用户: ${user.name}
</div>
</c: if >
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*" isELIgnored="false"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
< c:if test = "${!empty user}" >
< div align = "center" >
当前用户: ${user.name}
</ div >
</ c:if >
< table align = 'center' border = '1' cellspacing = '0' >
< tr >
< td >id</ td >
< td >名称</ td >
< td >价格</ td >
< td >购买</ td >
</ tr >
< c:forEach items = "${products}" var = "product" varStatus = "st" >
< tr >
< td >${product.id}</ td >
< td >${product.name}</ td >
< td >${product.price}</ td >
< td >
< form action = "addOrderItem" method = "post" >
数量< input type = "text" value = "1" name = "num" >
< input type = "hidden" name = "pid" value = "${product.id}" >
< input type = "submit" value = "购买" >
</ form >
</ tr >
</ c:forEach >
</ table >
|
步骤 8 : 测试
访问登陆页面,输入账号密码: tom: 123
http: //127.0.0.1/login.jsp
|
更多内容,点击了解: https://how2j.cn/k/cart/cart-user/597.html