< form action =" / sampleServlet"给我例外 [英] <form action="/sampleServlet" giving me exception

查看:86
本文介绍了< form action =" / sampleServlet"给我例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的jsp中,如果我调用< form action =/ sampleServletmethod =getname =form1> ,我会得到以下异常:


http 404错误 - 找不到sampleServlet。我在
web.xml文件中设置sampleServlet,并设置了url-pattern to / sampleServlet。


为什么我得到404(未找到servlet)?

解决方案

当你在HTML中使用URL时,如果没有前导/他们是相对于当前URL(即当前页面显示)。前导/他们是相对于网站的根:

 < form action =/ context-path / sampleServlet> 

 < form action =sampleServlet> 

会做你想做的事。

我建议你动态地在动作路径中添加上下文。
示例(在JSP中):

 < form action =$ {pageContext.request.contextPath} / sampleServlet > 

通过这种方式,您不必更改路径,例如,如果移动文件或复制你的代码,或重命名你的上下文!


In my jsp if I call <form action="/sampleServlet" method="get" name="form1">, I get the following exception :

http 404 error--sampleServlet is not found.I set sampleServlet in web.xml file and url-pattern also set to /sampleServlet.

Why I am getting 404 (not found servlet.)?

解决方案

When you are using URL in HTML, without leading / they are relative to the current URL (ie current page displayed). With leading / they are relative to the website root :

<form action="/context-path/sampleServlet">

or

<form action="sampleServlet">

will do what you want.

I suggest you to add the context inside the action path dynamically. Example (in JSP) :

<form action="${pageContext.request.contextPath}/sampleServlet">

With this you will never have to change the path, for example, if you move your file or copy your code, or rename your context!

这篇关于&lt; form action =&quot; / sampleServlet&quot;给我例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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