[英]How to get servlet action (request parameters) from index.html
I have in index.html: 我在index.html中:
<li><a href="list?action=list">List</a></li>
and in servlet class 并在servlet类中
public class Servlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
if (action.equalsIgnoreCase("List")){
// do something.....
}
}
}
web.xml web.xml
<servlet>
<servlet-name>Servlet</servlet-name>
<servlet-class>ru.proj.top.web.Servlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Servlet</servlet-name>
<url-pattern>/list</url-pattern>
</servlet-mapping>
action in servlet is null. Servlet中的操作为null。
How can I send this parameter from index.html
to servlet? 如何将此参数从index.html
发送到servlet?
I wouldn't name a servlet "Servlet" for test purposes. 为了测试目的,我不会将servlet命名为“ Servlet”。 May be, somewhere in the big world there exists a base class with the same name... 可能是,在这个大世界的某个地方,存在一个同名的基类...
So I call it "SimpleServlet". 所以我称它为“ SimpleServlet”。
Then you have this web.xml
(be aware of the fully qualified class name): 然后,您具有以下web.xml
(注意完全限定的类名):
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>simpleServlet</servlet-name>
<servlet-class>de.so.SimpleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>simpleServlet</servlet-name>
<url-pattern>/list</url-pattern>
</servlet-mapping>
</web-app>
Now, if you call http://localhost:8080/simpleServlet/list?action=demo
, the variable action
in doGet()
will contain "demo" 现在,如果您调用http://localhost:8080/simpleServlet/list?action=demo
,则doGet()
的变量action
将包含“ demo”
Also, I suggest to check action
for null before calling a method on it: 另外,我建议在对方法调用之前检查action
是否为空:
String action = req.getParameter("action");
if (action != null)
{
// do something
}
else
{
// do something else
}
From your question it looks like that the url mapped to the Servlet with <url-pattern>
in web.xml is different from what you are using in the href
attribute of <a/>
element in the index.html. 从您的问题来看,似乎在web.xml中使用<url-pattern>
映射到Servlet的url与您在index.html的<a/>
元素的href
属性中使用的<url-pattern>
不同。 After changing that url mapping to the consistent one, the code should work. 在将该URL映射更改为一致的URL映射之后,该代码应该可以工作。
You can add this value to the href
attribute to work seamlessly if you use a JSP page instead of simple html page. 如果使用JSP页面而不是简单的html页面,则可以将此值添加到href
属性以无缝工作。 This will dynamically add the context path of the application to the url and will prevent you from statically typing your context path name. 这会将应用程序的上下文路径动态添加到url中,并防止您静态键入上下文路径名。
<%= request.getContextPath() %>/list?action=list
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.