How can I access a parameter from a Servlet into .jsp file?? I am creating a simple web application and all of the tutorials, as well as the one I'm following to build the application encourage me to use just ${parameter} in the .jsp file, but it does not work.
I've been browsing through the answers and the only one I get is either to access this parameter through ${} or through <% request.getAttribute("name", name) %> which I don't want to be using, how can I make ${} work?? (I want it to be printed in welcome.jsp file after I put the parameters into a in a index.jsp file)
My code is as following:
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>
welcome.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>
What am I missing?
Aucun commentaire:
Enregistrer un commentaire