其他分享
首页 > 其他分享> > 在进行servlet复用时遇到的问题

在进行servlet复用时遇到的问题

作者:互联网

问题:

req.getRequestDispatcher("/jsp/pwdmodify.jsp").forward(req,resp);这句加在doGet()里面会报错,但单独跟一个方法就没出错。

解决:

​ 待解决。。。

package cn.laoyao.servlet;

import cn.laoyao.pojo.User;
import cn.laoyao.service.user.UserServiceImpl;
import cn.laoyao.util.Constants;
import com.alibaba.fastjson.JSONArray;
import com.mysql.jdbc.StringUtils;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

public class UpdateUserPasswordServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object method = req.getParameter("method");


        if(method != null && ((String) method).equals("savepwd")){
                updatePwd(req,resp);
        }else {
            req.setAttribute("message","新密码有问题");
        }
        
        
        //为啥这一句不管放在哪里都会报错
            req.getRequestDispatcher("/jsp/pwdmodify.jsp").forward(req,resp);


        if(method != null && ((String) method).equals("pwdmodify")){
            checkOldPwd(req,resp);
        }
        

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }





    //修改密码
    public void updatePwd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String newpassword = req.getParameter("newpassword");

        User user = (User) req.getSession().getAttribute(Constants.USER_SESSION);
        int flag = 0;
        if (user != null && !StringUtils.isNullOrEmpty(newpassword)) {
            flag = new UserServiceImpl().updatePassword(user, newpassword);
        }
        if (flag == 1){
            req.setAttribute("message","修改成功");
            req.getSession().setAttribute(Constants.USER_SESSION,null);
//                req.getSession().removeAttribute(Constants.USER_SESSION);
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
        }else {
            req.setAttribute("message","修改失败");
        }

    }


    //验证旧密码
    public void checkOldPwd(HttpServletRequest req, HttpServletResponse resp){
        String oldpassword = req.getParameter("oldpassword");
        Object o = req.getSession().getAttribute(Constants.USER_SESSION);

        //万能的MAP:结果集
        Map<String,String> resultMap = new HashMap<>();

        if(o != null){
            if(oldpassword == null){
                resultMap.put("result","error");
            }else if(((User)o).getUserPassword().equals(oldpassword)){
                resultMap.put("result","true");
            }else {
                resultMap.put("result","false");
            }
        }else{
            resultMap.put("result","sessionerror");
        }

        /*
            resultMap = {result=false}
            Json格式:{key:value}
        * */
        //手动变成Json格式
        String resultJson = resultMap.toString().replace("=","\":\"").replace("{","{\"").replace("}","\"}");
        resp.setContentType("application/json");
        try {
            PrintWriter writer = resp.getWriter();
            writer.write(resultJson);

            //使用ali的这个会卡在这,不知道为啥
//            System.out.println(JSONArray.toJSONString(resultMap));
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


}

标签:遇到,resp,req,复用,resultMap,import,servlet,method
来源: https://www.cnblogs.com/zlaoyao/p/16398807.html