今天做前端项目时写入jsp文件后,用的不是ssm框架,网页跳转写入的jsp文件的时候报错,网上也查了一些解决方案还是没解决,有没有遇到过这种情况的
这是包结构
tocmcat控制台报错如下
报错的过滤器代码如下
package com.his.filter;
import com.his.domain.User;
import com.his.util.LoginUtil;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebFilter("/*")
public class LoginFilter extends HttpFilter {
String[] excludes = new String[]{"login.jsp", "login", "timeout.jsp"
, "exit", "*.js", "*.css", "*.main.jsp", "*.png", "*.jpg", "verifyCode"
, "*.eot", "*.svg", "*.ttf", "*.woff", "*.woff2", "forget.jsp", "forget"
, "updatePwd.htm", "mailTip.jsp", "updatePwd"};
@Override
protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
// request.getRequestURI();
// request.getRequestURL();
// request.getServletPath();
String path = request.getServletPath();
for (String exclude : excludes) {
if (path.startsWith("/")) {
path = path.substring(1);
}
if (exclude.startsWith("*")) {
exclude = exclude.substring(1);
if (path.endsWith(exclude)) {
chain.doFilter(request, response);
return;
}
} else {
if (exclude.equals(path)) {
chain.doFilter(request, response);
return;
}
}
}
HttpSession session = request.getSession();
User user = (User) session.getAttribute("loginUser");
if (user != null) {
chain.doFilter(request, response);
} else {
if (LoginUtil.isAutoLogin(request,response)){
chain.doFilter(request,response);
return;
}
request.getRequestDispatcher("timeout.jsp").forward(request, response);
}
}
@Override
public void destroy() {
}
}
package com.his.filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class EncodingFilter extends HttpFilter {
@Override
protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("utf-8");
chain.doFilter(request, response);
}
String encoding;
@Override
public void init() throws ServletException {
encoding = this.getInitParameter("encoding");
if (encoding == null || "".equals("encoding")) {
encoding = "utf-8";
}
}
@Override
public void destroy() {
}
}
dao实现类的代码
package com.his.dao.Impl;
import com.his.dao.UserDao;
import com.his.domain.User;
import com.his.util.SqlSessionFactoryUtil;
import com.his.util.StringUtil;
import org.duyi.jdbc.SqlSession;
import org.duyi.jdbc.SqlSessionFactory;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class UserDaoImpl implements UserDao {
@Override
public User findByUname(String uname) {
String sql = "" +
"select " +
" uid,uname,upass,zjm,phone,mail,sex,age, " +
" create_time,create_uid,update_time,update_uid, " +
" delete_flag,yl1,yl2,yl3,yl4 " +
"from " +
" t_user " +
"where " +
" uname=#{uname} or zjm=#{uname} or phone=#{uname} or mail=#{uname}";
;
SqlSessionFactory factory = new SqlSessionFactory();
SqlSession session = factory.getSession(true);
Map<String, String> param = new HashMap<>();
param.put("uname", uname);
return session.selectOne(sql, param, User.class);
}
@Override
public void updatePwd(long uid, String upass) {
String sql = "update t_user set upass=#{upass} , update_time=now() , update_uid=#{uid} where uid = #{uid}";
SqlSessionFactory factory = new SqlSessionFactory();
SqlSession session = factory.getSession(true);
Map<String,Object> param = new HashMap<>();
//为了规避底层可能会存在的一些不足,在一个参数需要使用多次的情况下,需要组成map和对象
param.put("uid",uid);
param.put("upass",upass);
session.update(sql,param);
session.close();
}
@Override
public User findById(long uid) {
String sql="" +
" select " +
" uid,uname,zjm,truename,upass,phone,mail,sex,age, " +
" create_time,create_uid,update_time,update_uid, " +
" delete_flag,yl1,yl2,yl3,yl4 " +
" from t_user where uid = #{uid}";
SqlSessionFactory factory = new SqlSessionFactory();
SqlSession session = factory.getSession(true);
Map<String,Long> param = new HashMap<>();
//为了规避底层可能会存在的一些不足,在一个参数需要使用多次的情况下,需要组成map和对象
param.put("uid",uid);
return session.selectOne(sql,param,User.class);
}
@Override
public List<User> list(Map<String, Object> param) {
// 在页面展示时,除了要展示用户自己的信息(一条记录)以外
// 还要展示具体的创建人和修改人。
// 而数据库中存储的都是创建人id和修改人的id
// 按照正常逻辑思考,记录中只有创建人id,我们只需要用根据这个id找一下对应的人,就是知道创建人是谁了
/*
select * from t_user
sql中的* 等价于 java中的u 表示一条用户记录
for(User u : users){
用当前这条记录的uid在通过select语句查询一下对应的name
select uname from t_user where uid = u.getUid();
}
*/
String sql = "" +
" select " +
" u.*," +
" ifnull((select uname from t_user where uid=u.create_uid),'系统管理员') as create_uname , " +
" ifnull((select uname from t_user where uid=u.update_uid),'') as update_uname " +
" from " +
" t_user u " +
" where u.delete_flag = 1 ";
sql = appendSqlWhere(sql,param);
//做一个排序,我们想按照时间从前向后排序
//但有一个问题: 我们有2个时间 create_time , update_time
//对于一条记录而言,update > create
//但一条记录不一定有update
//所以我们想要的是,有update就用update排序,没有update就用create排序
sql += " order by ifnull(u.update_time,u.create_time) desc ";
sql += " limit #{start} , #{length}";
//SqlSessionFactory factory = new SqlSessionFactory();
SqlSession session = SqlSessionFactoryUtil.getDefaultFactory().getSession(true);
return session.selectList(sql,param,User.class);
}
@Override
public long listTotal(Map<String, Object> param) {
String sql = "" +
" select " +
" count(*) " +
" from " +
" t_user u " +
" where u.delete_flag = 1 ";
sql = appendSqlWhere(sql,param);
//SqlSessionFactory factory = new SqlSessionFactory();
SqlSession session = SqlSessionFactoryUtil.getDefaultFactory().getSession(true);
return session.selectOne(sql,param,Long.class);
}
private String appendSqlWhere(String sql, Map<String, Object> param) {
String uname = (String) param.get("uname");
if(StringUtil.isNotEmpty(uname)){
//有uname这个条件 可能用户名,也可能是助记码
sql += " and (u.uname like concat(#{uname},'%') or u.zjm like concat(#{uname},'%')) ";
}
String phone = (String) param.get("phone");
if(StringUtil.isNotEmpty(phone)){
sql += " and u.phone like concat(#{phone},'%') ";
}
return sql ;
}
}
service实现类的代码
package com.his.service.Impl;
import com.his.dao.Impl.UserDaoImpl;
import com.his.dao.UserDao;
import com.his.domain.User;
import com.his.service.UserService;
import com.his.vo.PageVO;
import java.util.List;
import java.util.Map;
public class UserServiceImpl implements UserService {
UserDao dao = new UserDaoImpl();
@Override
public User findByUname(String uname) {
return dao.findByUname(uname);
}
@Override
public void updatePwd(long uid, String upass) {
dao.updatePwd(uid,upass);
}
@Override
public PageVO list(Map<String, Object> param) {
//要做的是分页过滤查询
//过滤条件在param中直接使用,有就用,没有就拉倒
//分页数据中我们先确保page页码是正确的
// 页码有可能超标
//处理下限
Integer page = (Integer) param.get("page");
Integer rows = (Integer) param.get("rows");
page = Math.max(1,page);
//处理上限
//一共有多少页,上限是多少,不确定,需要算一下 total/rows 100/9=11+1=12页
//如果要计算,就需要total,所以需要先从数据库中查询总数,再计算。
//注意: 我们这次列表查询不仅有分页,还有过滤
// 所以我们要查的是在这个过滤条件的基础上,总页数
// 查询时需要携带过滤参数,参数都在param中
//注意:获取总数后,有一种可能,没有记录0,此时计算的最大页=0,需要将最大页变成1
long total = dao.listTotal(param);
int max = (int) (total%rows==0? total/rows : (total/rows + 1));
max = Math.max(1,max);
page = Math.min(page,max);
//更新一下
param.put("page",page);
//数据库查询数据
//根据文档分析,需要将功能分页的参数 转换成 数据库分页的参数 page+rows --> start+length
int start = (page-1)*rows ;
int length = rows ;
param.put("start",start);
param.put("length",length);
List<User> users = dao.list(param);
//接下来就是将数据返回给网页展示
//根据前端展示的需求需要返回list,page,total,rows,param(过滤条件)
//如何通过一个返回值携带多个信息呢
// 1. 多个信息组装成map (适合临时结构)
// 2. 单独创建一个类 (适合经常使用结构)
return new PageVO(users,total,rows,page,param,max);
}
}
controller层
package com.his.controller;
import com.his.service.Impl.UserServiceImpl;
import com.his.domain.User;
import com.his.service.UserService;
import com.his.vo.PageVO;
import org.duyi.mvc.ModelAndView;
import org.duyi.mvc.annotations.RequestMapping;
import org.duyi.mvc.annotations.RequestParam;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
public class UserController {
private static final int DEFAULT_PAGE = 1 ;
private static final int DEFAULT_ROWS = 10 ;
private UserService service = new UserServiceImpl() ;
@RequestMapping("/user/updatePwd")
public String updatePwd(@RequestParam("opass") String opass
, @RequestParam("upass") String upass
, @RequestParam("repass") String repass, HttpSession session){
if (upass==null||"".equals(upass)){
}
if (repass==null||"".equals(repass)){
}
if (opass==null||"".equals(opass)){
}
User user = (User) session.getAttribute("loginUser");
if(!user.getUpass().equals(opass)){
//原密码不正确
return "/view/user/updatePwdSuccess.jsp?f=9" ;
}
//原密码正确,接下来判断两次新密码是否一致,也可以在前端测试
if(!upass.equals(repass)){
//两次密码不一致
return "/view/user/updatePwdSuccess.jsp?f=8" ;
}
//修改密码了
user.setUpass(upass);
service.updatePwd(user.getUid(),upass);
return "/view/user/updatePwdSuccess.jsp" ;
}
@RequestMapping("/user/list")
public ModelAndView list(@RequestParam("page") Integer page , @RequestParam("rows") Integer rows
, @RequestParam("uname") String uname , @RequestParam("phone") String phone){
if(page == null){
page = DEFAULT_PAGE ;
}
if(rows == null){
rows = DEFAULT_ROWS ;
}
Map<String,Object> param = new HashMap<String,Object>();
param.put("page",page);
param.put("rows",rows);
param.put("uname",uname);
param.put("phone",phone);
PageVO vo = service.list(param);
//转发访问网页并携带数据
//我们的mvc框架如何实现上述需求
ModelAndView mv = new ModelAndView();
mv.setViewName("/view/user/list.jsp");
mv.setAttribute("vo",vo);
return mv ;
}
}