您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
08-案例-用户登录(jsp内置pageContextt的妙用-不用servlet直接获取request的attribute或session的attribute)
发布时间:2024-12-06 17:54:50编辑:雪饮阅读()
-
需求是将之前的品牌列表案例与登录注册案例结合,登录成功进入到品牌列表,品牌列表顶上显示当前用户名,登录失败就在登录页显示登录失败的错误信息。
那么其实可以参考这里
http://www.gaojiupan.cn/manshenghuo/chengxurensheng/5773.html
以及这里
http://www.gaojiupan.cn/manshenghuo/chengxurensheng/5762.html
进行一下整合呀。
不过咱这边说的第一个注意事项就是我们之前每次写表单提交的action都要手动补齐当前项目的contextPath,虽然也可以使用servlet去获取并用request的setAttribute给jsp页面使用。
那么有时候不能因为这个小东西,还单独写一个servlet吧,其实jsp页面默认内置有,如
<form action="${pageContext.request.contextPath}/loginServlet" id="form">
所以之前的这个login.html就需要修改为login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
<link href="css/login.css" rel="stylesheet">
<style>
.msg{
color:red;
}
</style>
</head>
<body>
<div id="loginDiv">
<form action="${pageContext.request.contextPath}/loginServlet" id="form">
<h1 id="loginMsg">LOGIN IN</h1>
<div class="msg">${msg}</div>
<p>Username:<input id="username" name="username" type="text"></p>
<p>Password:<input id="password" name="password" type="password"></p>
<div id="subDiv">
<input type="submit" class="button" value="login up">
<input type="reset" class="button" value="reset">
<a href="register.html">没有账号?点击注册</a>
</div>
</form>
</div>
</body>
</html>
还有就是我们现在是基于三层架构的,所以登录逻辑我觉得应该只返回一个user对象,所以封装到用户的service中比较合理。
package com.service;
import com.mapper.UserMapper;
import com.pojo.User;
import com.util.SessionFactoryUtil;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
public class UserService {
SqlSessionFactory sqlSessionFactory= SessionFactoryUtil.getSqlSessionFactory();
public User login(HttpServletRequest req) throws IOException {
String resource="mybatis-config.xml";
InputStream inputStream= Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession=sqlSessionFactory.openSession();
UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
String username=req.getParameter("username");
byte[] bytes=username.getBytes(StandardCharsets.ISO_8859_1);
username=new String(bytes,StandardCharsets.UTF_8);
String password=req.getParameter("password");
User user=userMapper.select(username,password);
sqlSession.close();
return user;
}
}
然后登录的servlet应该在登录失败的时候将错误消息通过request的setAttribute返回,登录成功则由session的setAttribte存储。
package com.web;
import com.pojo.User;
import com.service.UserService;
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.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
UserService userService=new UserService();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
User user=userService.login(req);
resp.setContentType("text/html;charset=utf-8");
if(user==null){
req.setAttribute("msg","用户名或密码错误");
req.getRequestDispatcher("/login.jsp").forward(req,resp);
}
else{
HttpSession session= req.getSession();
session.setAttribute("user",user);
resp.sendRedirect(req.getContextPath()+"/ServletQueryAll");
}
}
}
那么对于品牌列表我们要获取session中存储的user对象,我们也可以不需要通过servlet给设置到request的attribute中,也直接可以使用jsp内置语法得到session对象进一步获取到session中的user对象,所以brand.jsp则如
<%--
Created by IntelliJ IDEA.
User: 1
Date: 2024/12/2
Time: 15:15
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<p>欢迎你,${pageContext.request.getSession().getAttribute("user").username}</p>
<a href="addBrand.jsp">添加品牌</a>
<table border="1" cellspacing="0" width="800">
<tr>
<th>序号</th>
<th>索引</th>
<th>ID</th>
<th>品牌名称</th>
<th>企业名称</th>
<th>排序</th>
<th>品牌介绍</th>
<th>状态</th>
<th>操作</th>
</tr>
<c:forEach items="${brands}" var="brand" varStatus="status">
<tr>
<th>${status.count}</th>
<th>${status.index}</th>
<th>${brand.id}</th>
<th>${brand.brandName}</th>
<th>${brand.companyName}</th>
<th>${brand.ordered}</th>
<th>${brand.description}</th>
<th>
<c:if test="${brand.status == 1}">
启用
</c:if>
<c:if test="${brand.status == 0}">
禁用
</c:if>
</th>
<th>
<a href="/threeTierArchitectureModule/BrandUpdateById?id=${brand.id}">修改</a>
</th>
</tr>
</c:forEach>
</table>
</body>
</html>
关键字词:pageContextt,jsp,request,session,attribute
相关文章
- 06-小结(cookie最大存储3kb session无限制)
- 05-Session原理&细节(解决No plugin found for prefix
- 04-Session-基本使用
- 08-案例-查询所有(当jsp页面没有声明taglib时候JSTL的
- 03-JSP缺点
- 02-JSP脚本(类php中的html与动态代码混写且jsp修改无
- 01-JSP概述&快速入门&原理(jsp-servlet的继承与实现流
- 13-SqlSessionFactory工具类抽取(代码复用与性能优化)
- 07-Request请求转发(servlet转发及webapp目录下的资源
- 03-Request通用方式获取请求参数(servlet在get请求与p