Matt
Matt

Reputation: 8942

passing variable from servlet to jsp doesn't work

I have problem with passing an object from servlet to jsp.

Servlet.java

    @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    Controller test = new Controller();
    test.setObjects();
    request.getSession().setAttribute("item", test.node_1);
    request.getRequestDispatcher("index.jsp").forward(request, response);
}

index.jsp

<title> ${item.firstName} </title> 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <servlet>
        <servlet-name>Servlet</servlet-name>
        <servlet-class>socialgraphui.Servlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Servlet</servlet-name>
        <url-pattern>/Servlet</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

Tomcat output

okt 25, 2014 6:17:14 PM org.apache.catalina.startup.HostConfig deleteRedeployResources
    INFO: Undeploying context []
    okt 25, 2014 6:17:14 PM org.apache.catalina.startup.HostConfig deployDescriptor
    INFO: Deploying configuration descriptor /Library/Java/Servers/apache-tomcat-7.0.42/conf/Catalina/localhost/ROOT.xml
    okt 25, 2014 6:17:14 PM org.apache.catalina.util.LifecycleBase start
    INFO: The start() method was called on component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]] after start() had already been called. The second call will be ignored.

In browser console is no error or warning message. Could you please help me how can i find out what is wrong?

Upvotes: 2

Views: 2063

Answers (3)

njjnex
njjnex

Reputation: 1555

Check URL you follow it should be:

http://localhost:8080/your project name/Servlet

Also if item.getFirstName returns null you will see nothing in tags

To be sure that your param passing to jsp page change item value for String for example

request.getSession.setAttribute('item' , 'my title');

Now if you will see my title in <title> tag than passing param done and reason in your object.

Upvotes: 3

tmarwen
tmarwen

Reputation: 16354

Make sure your JSP is allowed to access the Session request object by setting below parameter on top of your JSP page:

<%@ page session="true" %>

And to avoid HTML illegal chars, it would be better to use :

<title>
  <c:out value="${sessionScope.item.firstName}"/>
</title>

Note: To use the c (core) JSTL, you must import the tag library by declaring below code on top of you JSP page:

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

Upvotes: 2

Sas
Sas

Reputation: 2503

You are setting the object in the session scope. So you may wanna use sessionscope to retrive the value:

<title>${sessionScope.item.firstName}</title>

Upvotes: 0

Related Questions