简体   繁体   English

如何从index.html获取servlet动作(请求参数)

[英]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.

相关问题 如何创建从Netbeans中的Java servlet到index.html或其他页面的链接? - How Do i create a link from a Java servlet in Netbeans back to a index.html or another page? 如何同时支持index.html和“ /” Servlet映射 - How to Support both index.html and “/” Servlet Mapping 如何从servlet初始化方法获取请求参数 - How to get request parameters from a servlet init method index.html未呈现Servlet 3.0 Tomcat 7 - index.html not rendering servlet 3.0 Tomcat 7 index.html未显示(不支持请求方法“ GET”) - index.html is not showing up (Request method 'GET' not supported) Spring Dispatcher servlet找不到index.html。 在DispatcherServlet中找不到带有URI []的HTTP请求的映射 - Spring dispatcher servlet cannot find index.html . No mapping found for HTTP request with URI [] in DispatcherServlet Java Servlet POST操作未从请求接收参数 - Java Servlet POST action not receiving parameters from request 如何使用参数将请求从android发送到servlet - How to send request from android to servlet with parameters index.html被忽略还是默认servlet优先于index.jsp和index.html? 为什么? - index.html ignored or default servlet got priority over index.jsp and index.html? Why? Servlet的ArrayList返回起点后将替换旧的输入值(index.html) - ArrayList from a servlet replacing old input values after going back to the start (index.html)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM