从JSP页面的表单操作中调用servlet [英] Calling servlet from a JSP page's form action
问题描述
我试图将输入到JSP页面中文本框中的值传递给servlet,该servlet将值存储为变量.但是,当我单击提交"按钮时,找不到servlet.我收到一条错误消息,指出the requested resource is not available
I'm trying to pass a value input to a textbox in a JSP page to a servlet that will store the value as a variable. But when I click the submit button the servlet isn't found. I get an error stating the requested resource is not available
Servlet类:
//parse input from hello.jsp input box
//and assign to fibNum variable
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
我查看了一些类似的问题:< form action =" /sampleServlet"给我例外,但是改变路径并不会改变结果.
I've looked at some questions like this: <form action="/sampleServlet" giving me exception but changing the path didn't change the outcome.
有人知道如何解决这个调用servlet的问题吗?还是在链接servlet时缺少我一个步骤?
Does anyone know how to fix this problem calling of a servlet? Or is there a step I'm missing in linking up the servlet?
这也是我的项目树的结构:
Also this is the structure of my project tree:
推荐答案
您需要在此处. 因此,在您的web.xml中定义;
You need to create servlet mapping in your web.xml. See here as well. So in your web.xml define;
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class><package name>.HelloServlet</servlet-class>
</servlet>
然后为该servlet创建映射(URL模式).
Then create mappings (url patterns) for the servlet.
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/say_hello/*</url-pattern>
</servlet-mapping>
现在在您的JSP参考中,像
Now in your JSP refernce the servlet like
<form action="say_hello" method="get">
<b>Fibonacci Sequence Length </b> <br>
<input type="text" name="fibNum"size="20px" style="font-size:30pt;height:60px" >
<input type="submit" value="submit" style="font-size:30pt;height:60px" > <br>
Value [1-100]<br>
</form>
这篇关于从JSP页面的表单操作中调用servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!