Question

I need to recall a few things on jsp for personal reasons :) I've got a trivial login page:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
</heaf>
<body>
    <form action="LoginServlet.do" method="POST">
        <p>
            First name: <input type="text" size="20" name="first">
        </p>
        <p>
            Last name: <input type="text" size="20" name="last">
        </p>
        <input type="submit" value="send">
    </form>
</body>
</html>

Bean class:

package web.model;

public class User {
    private String firstName;
    private String lastName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

I gather data from the form, create User instance, set it as a request attribute and forward the request to the result.jsp page:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        HttpSession session = request.getSession();     
        User user = new User();
        user.setFirstName(request.getParameter("first"));
        user.setLastName(request.getParameter("last"));
        request.setAttribute("user", user);
        RequestDispatcher view = request.getRequestDispatcher("result.jsp");
        view.forward(request, response);
    }

page code:

<%@ page 
    language="java" 
    contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"
    import="web.utils.Utils, web.model.User"
%>
<!DOCTYPE html>
<html>
<head>
<title>Result</title>
</head>
<body>
<%= ((User) request.getAttribute("user")).getFirstName() %>
<jsp:useBean id="user" class="web.model.User" scope="request" />
<jsp:getProperty property="user" name="firstName" />
</body>
</html>

the scriptlet code works correctly, stacktrace:

SEVERE: Servlet /Web threw load() exception
org.apache.jasper.JasperException: file:/result.jsp(15,0) jsp:getProperty for bean with name 'firstName'. Name was not previously introduced as per JSP.5.3
    at org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1110)
    at org.apache.jasper.compiler.Node$GetProperty.accept(Node.java:1125)
    at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2375)
    at org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2427)
    at org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2433)
    at org.apache.jasper.compiler.Node$Root.accept(Node.java:474)
    at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2375)
    at org.apache.jasper.compiler.Generator.generate(Generator.java:3517)
    at org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:250)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:373)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
    at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.init(JspServlet.java:138)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3954)
    at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:426)
    at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1345)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1530)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1519)
    at java.lang.Thread.run(Thread.java:722)

thanks in advance

Was it helpful?

Solution

Please change the usebean

<jsp:useBean id="user" class="web.model.User" scope="request" />
<jsp:getProperty property="firstName" name="user" />

in getProperty the name is the id value of the usebean.

check this http://profesores.elo.utfsm.cl/~agv/elo330/2s03/projects/Tomcat/doc/1.2/syntaxref1216.html#8856

OTHER TIPS

You mismatched the name and property attributes. name is the id (yes) of the bean, and property is the property of the bean you want to get:

<jsp:getProperty property="firstName" name="user" />

But, more importantly, you're coding your JSPs as if we were still in 1999. Since then, the JSP EL and the JSTL have been introduced, and scriptlets and jsp:xxx tags shouldn't be used anymore in JSPs. Your JSP should simply be:

<%@ page 
    language="java" 
    contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"
%>
<!DOCTYPE html>
<html>
  <head>
    <title>Result</title>
  </head>
  <body>
    ${user.firstName}
  </body>
</html>

Or, even better, you should be using the JSTL and make sure special characters in the firstName are properly escaped:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
...
<c:out value="${user.firstName}"/>

check this line

<jsp:useBean id="user" class="web.model.User" scope="request" />
<jsp:getProperty property="user" name="firstName" />

it should be

<jsp:useBean id="user" class="web.model.User" scope="request" />
<jsp:getProperty property="*" name="user" />

note * for all the properties

but for your case it should be

<jsp:useBean id="user" class="web.model.User" scope="request" />
<jsp:getProperty property="firstName" name="user" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top