我有简单的jsp应用程序。那就是使用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);
}
}
服务器已成功运行且应用程序正常运行。但是,我无法通过servlet设置属性。在网络浏览器中,我收到了:
Hello World! 您的IP地址是127.0.0.1。您的名字是空的
我读过这个 Cannot get attribute (in request scope) set by Servlet and forwarded to JSP 但它并没有帮助我。
感谢所有读到最后的人。我希望你能帮忙。 附:对不起我的英语不好。我来自乌克兰。
答案 0 :(得分:1)
我认为您阅读的question上的答案解释了这种情况,但没有告诉您究竟要使用哪个网址。 /processForm
是您必须请求以调用servlet的映射路径。
因此,如果您的网络应用程序的上下文路径为/web_application
,则您需要浏览/web_application/processForm
。
如果您单独浏览到/web_application
,您的容器将搜索welcome files,并且因为index.jsp
默认存在,您无需调用jsp即可请求jsp servlet的。因此,servlet中设置的请求属性将为null。