无法从doGet调用doPost

时间:2016-01-07 09:22:58

标签: java servlets

当我直接调用doPost时,它会以正确的方式显示我的个人资料页面。但是登录后我在Chrome复制网址“http://localhost:8080/17_servlets_jsp/profile”中打开了新标签,它显示了空白页

@WebServlet("/profile")
public class Profile extends HttpServlet {
**private String login;**
private HttpSession httpSession;
private User user;
private Role role;
public static Logger LOGGER = LogManager.getLogger(Profile.class.getName());

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setContentType("text/html");
    httpSession = req.getSession();
    try (PrintWriter out = resp.getWriter()) {
        **if (httpSession.getAttribute("userLoggedIn") == null) {
            out.println("<title>Login Page</title>");
            out.println("<p><a href=\"index\">Please follow the link to login</a></p>");
        } else {
            login = (String) httpSession.getAttribute("uLogin");
            doPost(req, resp);
        }**
    } catch (IOException | NullPointerException e) {
        LOGGER.error(e);
    }
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    **login = req.getParameter("login");
    user = new ImplUserDAO().findByLogin(login);**
    httpSession = req.getSession();
    resp.setContentType("text/html");
    try (PrintWriter out = resp.getWriter()) {
        out.println("<title>Profile page</title>");
        if (user != null && user.getPassword().equals(req.getParameter("pass"))) {
            role = new ImplRoleDAO().findById(user.getRoleId());
            httpSession.setAttribute("userLoggedIn", true);
            httpSession.setAttribute("uLogin",req.getParameter("login"));
            httpSession.setAttribute("uPass",req.getParameter("pass"));
            out.println("user id = " + user.getUserId());
            out.println("login = " + user.getLogin());
            out.println("password = " + user.getPassword());
            out.println("role = " + role.getRoleName());
            out.println("<form action=\"logout\" method=\"get\"/>" +
                    "<input type=\"submit\" value=\"Logout\"/>" +
                    "</form>");
            if (role.getRoleName().equals("admin")) {
                httpSession.setAttribute("isAdmin", true);
                out.println("<a href=\"admin\">Go to admin page</a>");
            }
        } else {
            out.println("Wrong login or password");
            out.println("<a href=\"index\">Please follow the link to login</a>");
        }
    } catch (IOException | NullPointerException e) {
        LOGGER.error(e);
    }
}
}

1 个答案:

答案 0 :(得分:0)

通过调用doPost(),您也可以执行以下行:

user = new ImplUserDAO().findByLogin(req.getParameter("login"));

由于您的Exception请求不包含该参数,因此会抛出null或返回GET或其他内容。如果您想重用doPost(),则需要从任一会话或请求中获取登录信息:

String login = req.getParameter("login");
if(null == login) {
  login = (String)httpSession.getAttribute("uLogin");
}

或类似。

关于属性和参数

在注释掉的代码中,您尝试通过在请求中调用setAttribute("login"...来解决此问题。虽然这是合法的,但您无法通过调用getParameter()来检索该值。相反,你必须再次调用'getAttribute()` - 直接从会话中检索没有多大区别:

//Retrieve Parameter (been send into the servlet from outside)
String login = req.getParameter("login");
if(null == login) {
  //Retrieve Attribute you put into the request in doGet()
  login = (String)req.getAttribute("login");
}

另一个编辑

您与req.getParameter("pass")存在同样的问题。

您应该认真考虑将登录过程(从请求中检查用户名/密码)与其他功能(显示配置文件页面)或反向逻辑拆分:登录,将用户设置为{{1如果成功调用doPost()

完整示例代码

考虑这些变化来修复你的逻辑:

goGet()