提交 HTML 表单后,servlet 操作出现在 URL 而不是 JSP 文件中

发布于 2024-11-05 09:43:20 字数 1403 浏览 1 评论 0原文

我创建了一个简单的登录页面。如果用户输入正确的用户名和密码,页面将被重定向到成功页面,否则它将被重定向到索引页面。在登录页面中,我向 servlet 提供了表单提交操作。一旦 servlet 验证输入,它将分派到相应的 jsp 页面。我的问题是操作名称在调度后仍然在 url 中。对吗?

package com.123.www;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Login extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public Login() {
        super();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {

        response.setContentType("text/html");

        //PrintWriter out = response.getWriter();

        String userName = request.getParameter("username");

        String passWord = request.getParameter("password");

        RequestDispatcher view = null ;

        if((userName=="")&&(passWord==""))
        {
            view = request.getRequestDispatcher("index.jsp");
        }
        else
        {   
            HttpSession session = request.getSession(true);
            session.setAttribute("name",userName);
            view = request.getRequestDispatcher("success.jsp");
        }

        view.forward(request, response);

    }

}

I created a simple login page. If the user entered right username and password the page ill be redirected to the success page otherwise it will be redirected to the index page. In login page i given the form submit action to the servlet. Once the servlet validates the input it will dispatched to the respective jsp page. My problem was the action name still in the url after the dispatch also. Is it right?

package com.123.www;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Login extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public Login() {
        super();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {

        response.setContentType("text/html");

        //PrintWriter out = response.getWriter();

        String userName = request.getParameter("username");

        String passWord = request.getParameter("password");

        RequestDispatcher view = null ;

        if((userName=="")&&(passWord==""))
        {
            view = request.getRequestDispatcher("index.jsp");
        }
        else
        {   
            HttpSession session = request.getSession(true);
            session.setAttribute("name",userName);
            view = request.getRequestDispatcher("success.jsp");
        }

        view.forward(request, response);

    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

极度宠爱 2024-11-12 09:43:20

调度发生在服务器端,而不是客户端。转发基本上告诉 servlet 容器使用哪个视图来呈现结果。它的位置确实不会出现在客户端的浏览器地址栏中。仅当您通过 response.sendRedirect() 使用重定向时才会发生这种情况。重定向基本上告诉网络浏览器在给定位置触发新的 GET 请求。此时浏览器地址栏将更改为新的 URL。

只需隐藏 /WEB-INF 文件夹中的视图(JSP 文件),以便最终用户不再直接访问它,并重用相同的 servlet 通过 显示登录表单doGet() 并继续处理通过 doPost() 提交的登录表单。如果您没有实现 doGet() 那么它将显示 HTTP 状态 405 - 此 URL 不支持 HTTP 方法 GET

@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Just show form.
        request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Process form submit.
        // ...

        if (success) {
            response.sendRedirect("home");
        } else {
            request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
        }
    }

}

这样你就可以通过 http://example.com/context/login 获取登录页面并提交到同一个 URL。

您可以借助 前端控制器模式<,通过单个 servlet 对所有其他 URL 执行相同的操作/a>.这只是一点工作,这也是 MVC 框架存在的原因 :)

另请参阅:

Dispatching happens server side, not client side. The forward basically tells the servletcontainer which view to use to present the result. Its location indeed doesn't appear in the client's browser address bar. This will only happen when you use a redirect instead by response.sendRedirect(). A redirect basically tells the webbrowser to fire a new GET request on the given location. Hereby the browser address bar will be changed to the new URL.

Just hide away the view (the JSP file) in /WEB-INF folder so that it cannot be directly accessed by the enduser anymore, and reuse the very same servlet to show the login form via doGet() and continue processing the login form submit via doPost(). If you don't implement doGet() then it would show HTTP Status 405 - HTTP method GET is not supported by this URL.

@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Just show form.
        request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Process form submit.
        // ...

        if (success) {
            response.sendRedirect("home");
        } else {
            request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
        }
    }

}

This way you can get the login page by http://example.com/context/login and submit to the same URL.

You can do the same for all other URLs by a single servlet with help of the front controller pattern. It's only a bit of work and that's also why MVC frameworks exist :)

See also:

辞别 2024-11-12 09:43:20

Servlet 有一些神奇的功能,称为转发。您应该使用 HTTP 重定向。当您通过 POST 方法提交表单时,此方法非常有用。然后,在您发送 HTTP 重定向页面地址后,用户尝试刷新页面时不会再次提交表单。

您听说过 Spring 框架吗?它为构建 Web 应用程序提供了很好的框架。

Servlets have some magic called forwarding. You should rather use HTTP redirect. This method is very usefully when you have form sumibteed by POST method. Then after you sent HTTP redirect page address is changed and user don't submit form second time when he tries to refresh page.

Have you ever hear about Spring Framework ? It provides great skeleton for builing web applications.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文