JSP转发并调用servlet(传递2个会话变量)
我正在尝试将“HTTPrequest”从 JSP 页面重定向到 servlet(位于包中);传递存储在会话对象中的变量。 我的想法是:
<jsp:forward page"/servletName">
<jsp:param name="var1" value="<%=beanID.getVar1()%>" />
<jsp:param name="var2" value="<%=beanID.getVar2()%>" />
</jsp:forward>
在 servlet 中,您可以找到带有 @override 注释的 doPost。使用以下代码:
public class servletName extends HttpServlet{
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
var1 = request.getParameter("var1").toString();
var2 = request.getParameter("var2").toString();
// do more with the variables.
}
运行项目时,参数被发送到调用重定向/转发的 JSP。存储变量后(并且必须调用 servlet [JSP:forward]),应用程序返回 404 页面不存在。
有人有想法吗? 如果需要澄清,请告诉我。
提前致谢! B.
I am trying to redirect a 'HTTPrequest' from a JSP page to a servlet (located in a package); passing on variables that are stored in a session object.
I had the idea to use:
<jsp:forward page"/servletName">
<jsp:param name="var1" value="<%=beanID.getVar1()%>" />
<jsp:param name="var2" value="<%=beanID.getVar2()%>" />
</jsp:forward>
In the servlet you can find a doPost with an @override annotation. With the following code:
public class servletName extends HttpServlet{
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
var1 = request.getParameter("var1").toString();
var2 = request.getParameter("var2").toString();
// do more with the variables.
}
When running the project, the parameters get sent to the JSP where the redirect / forward is called. After the variables have been stored (and where the servlet has to be called [JSP:forward]) the app returns a 404 Page does not exist.
Does anybody have an idea?
If clarification is needed, please tell me.
Thanks in advance!
B.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只要 URL 完全错误,或者 URL 后面实际上没有任何内容,就会出现 404。对于 servlet,当 servlet 未在
web.xml
中声明时,或者当 URL 与 servlet 的url-pattern
不匹配时,或者当构造时,最后一种情况可能会发生Servlet 的初始化失败(不过,这应该在服务器日志中可见)。查看
[servlets]
标签信息页面可能会有所帮助。有一个 Hello World 示例,它可能有助于提供有关使用 JSP/servlet 的新见解。请注意,您不会重定向至此处。您正在转发到 servlet。 Servlet 将获得与 JSP 完全相同的请求/响应。重定向基本上指示客户端创建新请求。
The 404 will occur whenever the URL is plain wrong, or there's actually nothing which sits behind the URL. In case of servlets, the last can happen when the servlet is not declared in
web.xml
, or when the URL does not match servlet'surl-pattern
, or when the construction and initialization of the servlet failed (this one should be visible in server logs however).It may help to take a look in
[servlets]
tag info page. There's a Hello World example which may help in giving the new insights about using JSP/servlets.Note that you aren't redirecting here. You are forwarding to a servlet. The servlet will obtain exactly the same request/response as the JSP. A redirect basically instructs the client to create a new request.
首先,您不需要将这些变量作为参数发送,您可以访问 servlet 中的会话对象 (request.getSession())。您确定正在调用 servlet 吗?也许您可以显示更多代码,servlet 中发生了什么。
first of all you don't need to send those variables as parameter you could access the session object within the servlet (request.getSession()). Are you sure that the servlet is being called ? Maybe you could show little more code what happens in the servlet.