2

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?

Matt
  • 8,195
  • 31
  • 115
  • 225
  • Seems fine to me, does your object has getters for the attribute you are asking or/and are you sure it has the name set? – KBorja Oct 25 '14 at 16:30
  • @KBorja Actually I am not sure if name of object is set. I tried to debug the application and set a breakpoint in Servlet.java class in doGet method, but it didn't stop at breakpoint. I am new in java ee and i am not sure if doGet method is called after project running. – Matt Oct 25 '14 at 16:45
  • @KBoja if object have no getter it will produce javax.el.PropertyNotFoundException for Matt to execute doGet method according your config you should follow this adress 'http://localhost:8080/your project name/Servlet' – njjnex Oct 25 '14 at 16:47
  • 1
    Make a little test, pass one string to the session so you can see if your server is working... – KBorja Oct 25 '14 at 18:05
  • possible duplicate of [Pass variables from servlet to jsp](http://stackoverflow.com/questions/3608891/pass-variables-from-servlet-to-jsp) – Gas Oct 25 '14 at 20:38
  • @KBorja I tried tmarwen's answer, but result is the same. In title is nothing. – Matt Oct 26 '14 at 08:04
  • @Gas Those answers didn't help me – Matt Oct 26 '14 at 08:06
  • @njjnex So if I want to execute doGet method, i have to use localhost:8080/my project name/Servlet address? Because i use localhost:8080 address nad when i want to use localhost:8080/my project name/Servlet I got http status 404 – Matt Oct 26 '14 at 08:10
  • It works with localhost:8080/Servlet address – Matt Oct 26 '14 at 08:22

3 Answers3

3

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.

njjnex
  • 1,490
  • 13
  • 23
2

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"%>
tmarwen
  • 15,750
  • 5
  • 43
  • 62
  • I tried it, but result is still the same. In title is nothing. – Matt Oct 26 '14 at 08:01
  • Coudl you provide a sample web app and host it in some remote code storage provider, Github may be. It would be easier to read. You may need to describe what your `item` object referer class is and what `node_1` is! – tmarwen Oct 26 '14 at 10:26
  • I am sorry i tried it with wrong address. It works with localhost:8080/Servlet address – Matt Oct 26 '14 at 20:23
  • Could you accept the answer and upvote it if it helped resolving the issue? – tmarwen Oct 26 '14 at 22:27
  • Thanks for help, but actually njjnex's answer helped me solve the problem, but i can at least upvote – Matt Oct 27 '14 at 08:08
0

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

<title>${sessionScope.item.firstName}</title>
Sas
  • 2,473
  • 6
  • 30
  • 47