提交HTML表单后,servlet操作将显示在URL中而不是JSP文件中 [英] After submitting HTML form, servlet action appears in URL instead of JSP file

查看:116
本文介绍了提交HTML表单后,servlet操作将显示在URL中而不是JSP文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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);

    }

}

推荐答案

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

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.

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

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);
        }
    }

}

这样,您可以通过 http://example.com/context/login并提交到相同的URL.

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

您可以在前面的帮助下,通过单个servlet对所有其他URL进行相同的操作.控制器模式.仅需一点工作,这也是MVC框架存在的原因:)

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 :)

  • Our Servlets wiki page
  • RequestDispatcher.forward() vs HttpServletResponse.sendRedirect()
  • doGet and doPost in Servlets
  • Design Patterns web based applications

这篇关于提交HTML表单后,servlet操作将显示在URL中而不是JSP文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆