Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

java - Why I get null when retrieving servlet's init parameter from JSP?

I have DD (web.xml file) with very simple code:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>TestProject</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
  <servlet-name>test</servlet-name>
  <jsp-file>/result.jsp</jsp-file>

  <init-param>
  <param-name>email</param-name>
  <param-value>[email protected]</param-value>
  </init-param>
  </servlet>
  
  <context-param>
  <param-name>name</param-name>
  <param-value>Max</param-value>
  </context-param>
  
</web-app>

Notice I have two parameters (one in application, other in configuration scope). When I try to get them inside result.jsp with:

<html><body>
Name is: <%=application.getInitParameter("name") %>
<br>
Email is: <%=config.getInitParameter("email") %>
</body></html>

, I get following output:

Name is: Max 
Email is: null 

My question is simple: how did I get NULL for "email" parameter? Shouldn't my JSP file "see" how I configured it and return "[email protected]"?

question from:https://stackoverflow.com/questions/65648862/why-i-get-null-when-retrieving-servlets-init-parameter-from-jsp

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Is that your entire web.xml file? And by any chance are you accessing the JSP directly in the browser? Like:

http://localhost:8080/<yourAppContext>/result.jsp

If that is the case, then you will get this response:

Name is: Max
Email is: null 

It is not wrong. It is correct.

The reason you get this result is that you are not accessing the JSP through the configuration you defined in web.xml, you are just accessing the JSP directly, which behind the scene has a different implicit configuration, and it's not the one you think you are configuring.

If you want this response:

Name is: Max
Email is: [email protected]

Then you need to add a servlet mapping. The complete configuration is:

<servlet>
    <servlet-name>test</servlet-name>
    <jsp-file>/result.jsp</jsp-file>

    <init-param>
        <param-name>email</param-name>
        <param-value>[email protected]</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>test</servlet-name>
    <url-pattern>/test</url-pattern>   
</servlet-mapping>

and you need to access this URL, not the JSP path, with:

http://localhost:8080/<yourAppContext>/test

You might want to also read these:

To further drive the point home, it's important to mention that you need a mapping for one of your servlets to be useful. If you just define a servlet in web.xml, it just sits there. You need to tell the server how to use it, and for that you use the <servlet-mapping>. It's saying to the server that for a request on a path, some specific servlet needs to be called to handle the request.

You can create this mapping to point to a servlet class using <servlet-class> or to a JSP using <jsp-file>. They are basically the same thing, since a JSP eventually becomes a servlet class.

What I think is confusing you (based on the comment below) is that for JSP files you already have some implicit mapping created by the server, as described here.

When you access the JSP directly, with

http://localhost:8080/<yourAppContext>/result.jsp

you are using the implicit server mapping which contains no special configuration attached (like the email you want to send to it).

When you access the JSP with

http://localhost:8080/<yourAppContext>/test

you are accessing your mapping. And this you can configure however you want, and send it whatever parameters you want, and your JSP will now be able to read them.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...