如何从servlet访问参数到.jsp文件?我正在创建一个简单的Web应用程序和所有教程,以及我关注的构建应用程序的一个,鼓励我在.jsp文件中仅使用$ {parameter},但它不起作用(在输出中它打印$ {name} $ {password}而不是值(。
我一直在浏览答案,我唯一得到的是通过$ {}访问此参数为了使用,我该如何使$ {}工作?(我希望将其打印在inter.jsp文件中,然后将参数放入index.jsp文件中(
我的代码如下:
loginservlet.java
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoginServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().append("Served at: ").append(request.getContextPath());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
String name = request.getParameter("name");
String password = request.getParameter("password");
String url = "/welcome.jsp";
HttpSession session = request.getSession();
session.setAttribute("name", name);
getServletContext().getRequestDispatcher(url).forward(request, response);
}
}
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>LoginServlet</servlet-name>
<display-name>LoginServlet</display-name>
<description></description>
<servlet-class>com.zurciu.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/logmein</url-pattern>
</servlet-mapping>
</web-app>
index.jsp
<html>
<body>
<form action="logmein" method="post">
<pre>
Login : <input type="text" name="name"> Password : <input type="password" name="password"> <input type="submit" value="submit">
</pre>
</form>
</body>
</html>
欢迎。JSP
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Welcome</title>
</head>
<body>
Welcome user!
${name}
${password}
</body>
</html>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>com.zurciu.maven</groupId>
<artifactId>JSP_ACCESS_PARAMETER</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>JSP_ACCESS_PARAMETER 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>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jsp-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>JSP_ACCESS_PARAMETER</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<verbose>true</verbose>
<source>1.8</source>
<target>1.8</target>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
<contextReloadable>true</contextReloadable>
</configuration>
</plugin>
</plugins>
</build>
</project>
我缺少什么?
您可以使用选项尝试使用JSTL而不是使用Direct EL。另外,未设置JSP页面范围,默认情况下,EL取决于请求范围,但是您可以通过基于$ {session.name}打印会话的值来单位测试代码的其余部分,请使用$ {request.name继续前进},如果这也有效,则意味着您的EL不会将请求范围作为默认值。希望这可以帮助!
好吧,我想我有答案。我相信解决此问题的解决方案是将Servlet Web.xml声明为3.0
我将Web.xml修改为此,现在可以使用:(
<web-app
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_3_0.xsd"
version="3.0">