0

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 (in the output it prints ${name} ${password} instead of the values).

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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Dominika
  • 187
  • 3
  • 13
  • It seems you are not including the `jstl` library or any similar that would help you manage your java variables inside your jsp using the `${}` – NickAth Jul 30 '19 at 14:25
  • "Doesn't work" is too general. Please describe what actually get: an error, an empty string, exception in code, something else? – M. Prokhorov Jul 30 '19 at 14:25
  • I get ${name} and ${password} in .jsp output, instead of the values I typed into these parameters – Dominika Jul 30 '19 at 15:27

2 Answers2

0

You can try with jstl using option instead of using direct EL. Also, jsp page scope is not set and by default EL takes request scope, but you can unit test rest of the code by printing session based value i.e. ${session.name}, if this works then move on with ${request.name}, if this works too, then it means that your EL is not taking request scope as default. Hope this helps!

Abhi
  • 94
  • 7
0

Ok, I think I have the answer. I believe the solution to this problem is to declare the Servlet web.xml to version 3.0

I modified web.xml to this and now it works :)

<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">
Dominika
  • 187
  • 3
  • 13