如何使用 JSP 和 Servlet 编辑我的页面 URL。

huangapple go评论58阅读模式
英文:

how can i edit the url of my page using JSP and Servlet

问题

这是我本地项目的 URL,是一个登录页面: "http://localhost:8080/OnlineExamination"

在登录验证成功后,我希望将 URL 更改为 "http://localhost:8080/OnlineExamination/home-page",这样在页面重新加载时就防止用户被带回登录页面。我该如何使用 JSP 和 Servlet 来实现这一点?我已经使用了一个提交类型的输入框,将输入框包在锚点标签中导致无法提交登录表单。请帮忙!

英文:

This is the url of my project on localhost which is a login page:: "http://localhost:8080/OnlineExamination"

Here as soon as the login is valid, I want the url to be changed to "http://localhost:8080/OnlineExamination/home-page" so that it would prevent the user from taking him back to login page when the page reloads. How can I do this using JSP and Servlet? I have used a submit type input in which surrounding input by anchor tag resulted in not submitting the login form. Please Help!!

答案1

得分: 1

# 重定向还是转发 - 我应该选择哪个?

因为您希望在浏览器中更改URL,所以您将不得不发送重定向指令到浏览器,而不是将请求转发到其他资源。请注意,当您转发请求时,新资源将获得来自请求的所有内容,而URL保持不变。另一方面,在重定向的情况下,浏览器会为新位置(URL)创建一个新的请求,这意味着旧请求的任何内容都将对新请求不可用。

# 我如何在请求之间共享一些信息?

您将不得不选择一个更大的范围,例如“会话”;只需将信息放入会话中,并在新位置检索相同的信息。

# 概念的示例:

**OnlineExamination.java**

    @WebServlet("/OnlineExamination")
    public class OnlineExamination extends HttpServlet {
    	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		response.setContentType("text/plain");
    		// 代码以检查用户凭据
    		// ...
    		// ...
    		request.getSession().setAttribute("loggedIn", true);
    		response.sendRedirect("OnlineExamination/home-page");
    	}
    }

**HomePage.java**

    @WebServlet("/OnlineExamination/home-page")
    public class HomePage extends HttpServlet {
    	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		response.setContentType("text/plain");
    		PrintWriter out = response.getWriter();
    		out.write("" + request.getSession().getAttribute("loggedIn"));
    	}
    }

这只是一个用于说明概念的非常基本的代码。在实际项目中,您可能会选择一些可以提供各种方法来执行所有这些操作的框架。
英文:

Redirect or Forward - which one should I choose?

Since you want the URL to be changed in the browser, you will have to send redirect instruction to the browser instead of forwarding the request to some other resource. Note that when you forward the request, the new resource will get everything from the request and the URL remains unchanged. On the other hand, in the case of redirect, the browser creates a new request for the new location (URL) which means nothing from the old request will be available to the new request.

How can I share some information between requests?

You will have to choose a bigger scope e.g. session; simply put the information into the session and retrieve the same at the new location.

An illustration of the concept:

OnlineExamination.java

@WebServlet("/OnlineExamination")
public class OnlineExamination extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/plain");
		// Code to check user credentials
		// ...
		// ...
		request.getSession().setAttribute("loggedIn", true);
		response.sendRedirect("OnlineExamination/home-page");
	}
}

HomePage.java

@WebServlet("/OnlineExamination/home-page")
public class HomePage extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/plain");
		PrintWriter out = response.getWriter();
		out.write("" + request.getSession().getAttribute("loggedIn"));
	}
}

This is a very basic code just for illustrating the concept. In a real project, you may choose some framework that can provide various ways to do all these.

答案2

得分: 0

你应该使用“解释器”来防止合法登录用户访问链接。如果你只是将用户重定向到主页,他们仍然可以访问登录页面。
或者你可以创建一个简单的函数,如果他们访问登录页面但已经登录,必须将他们重定向到主页。

英文:

You should use "interpreter" to prevent a valid login user to access a link. If you just only redirect user to homepage, they still can access the login page.
Or you just create a simple function that if they access login page but already logon, they must be redirect to homepage.

huangapple
  • 本文由 发表于 2020年8月15日 22:54:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63427366.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定