简体   繁体   中英

Can't get attribute in jsp forwarded by servlet

I have simple jsp application. That use Maven.

Pom.xml:

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>Lab3.web_application</groupId>
    <artifactId>web_application</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>web_application Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <dependencies>
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
      </dependency>

      <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
      </dependency>
    </dependencies>

    <build>
    <finalName>web_application</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>com.oracle.weblogic</groupId>
            <artifactId>wls-maven-plugin</artifactId>
            <version>12.1.3.0</version>
            <configuration>
                <middlewareHome>/home/bluebird/Oracle/Middleware/Oracle_Home</middlewareHome>
                <adminurl>t3://localhost:7001</adminurl>
                <domainHome>/home/bluebird/Oracle/Middleware/Oracle_Home/user_projects/domains/base_domain
                </domainHome>
                <user>weblogic</user>
                <password>yamahar6</password>
                <upload>true</upload>
                <remote>false</remote>
                <verbose>true</verbose>
                <source>./target/web_application.war</source>
                <name>${project.build.finalName}</name>
            </configuration>
            <executions>
                <execution>
                    <id>startserver</id>
                    <phase>initialize</phase>
                    <goals>
                        <goal>start-server</goal>
                    </goals>
                </execution>
                <execution>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>deploy</goal>
                    </goals>
                  </execution>
                </executions>
            </plugin>
         </plugins>
     </build>
  <distributionManagement>
    <snapshotRepository>
        <id>localSnapshot</id>
        <name>local snapshot repository</name>
        <url>file:///home/bluebird/.m2/repository</url>
    </snapshotRepository>
  </distributionManagement>
</project>

index jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head><title>Hello World ${requestScope.name}</title></head>
    <body>
        Hello World!<br/>
    <%
      out.println("Your IP address is " + request.getRemoteAddr());
      out.println("Your name is " +     request.getAttribute("name"));
     %>
    </body>
</html>

web.xml:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <servlet>
    <servlet-name>MainServlet</servlet-name>
    <servlet-class>MainServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>MainServlet</servlet-name>
    <url-pattern>/processForm</url-pattern>
  </servlet-mapping>

</web-app>

MainServlet.java:

@WebServlet("/processForm")
public class MainServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {


        req.setAttribute("name", "Devcolibri");
        req.getRequestDispatcher("index.jsp").forward(req, resp);


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

        super.doPost(req, resp);
    }

}

Server is successfully run and application works. But, I can't get attributes set by servlet. In web browser i received:

Hello World! Your IP address is 127.0.0.1 Your name is null

I have read this Cannot get attribute (in request scope) set by Servlet and forwarded to JSP but it didn't help me.

Thanks all who read to the end. I hope to you help. PS Sorry for my English. I am from Ukraine.

I think the answer on the question you read explains the situation, but stops short of telling you exactly what url to use. /processForm is the mapped path that you must request in order to invoke the servlet.

So if your web application's context path is /web_application , you'll want to browse to /web_application/processForm .

If you browse to /web_application alone, your container will search for welcome files , and because index.jsp is there by default, you'd be requesting the jsp without ever invoking the servlet. Hence, the request attributes set in the servlet would be null.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM