Reda93
Reda93

Reputation: 23

session.getAttribute from servlet and display in html

I have a login servlet that takes values from a login html file and redirects immediately to sessiondemo.html.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

  public class logIn extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse res)
                      throws ServletException, IOException {

      res.setContentType("text/html");
      PrintWriter out = res.getWriter();
      HttpSession session = req.getSession(true);

      //getting the value(s) from HTML
      String fname = req.getParameter("fname");
      String lname = req.getParameter("lname");
      String email = req.getParameter("email");
      String address = req.getParameter("address");

      //Storing the values in Session
      session.setAttribute("FirstName", fname );
      session.setAttribute("LastName", lname );
      session.setAttribute("Email", email);
      session.setAttribute("address", address);

      res.sendRedirect("http://localhost:8080/apps/sessiondemo.html");
      out.close(); 
    }
  }

What I cant figure out is, I want to display the first and last name in the sessiondemo html. I know the code has to be written in the seesiondemo html but it is not working.

Here is what I tried:

<% session.getAttribute("FirstName", fname);
        out.println("You are logged in as" + fname + "!");%>

XML files

<?xml version="1.0" encoding="ISO-8859-1"?>
<!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>
  <description>Test examples</description>
  <display-name>My Servlets</display-name>

 <servlet>
  <servlet-name>GET</servlet-name>
  <servlet-class>GET</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>GET</servlet-name>
  <url-pattern>/GET</url-pattern>
</servlet-mapping>

 <servlet>
  <servlet-name>POST</servlet-name>
  <servlet-class>POST</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>POST</servlet-name>
  <url-pattern>/POST</url-pattern>
</servlet-mapping>

 <servlet>
  <servlet-name>Cart</servlet-name>
  <servlet-class>Cart</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>Cart</servlet-name>
  <url-pattern>/Cart</url-pattern>
</servlet-mapping>

<servlet>
  <servlet-name>logIn</servlet-name>
  <servlet-class>logIn</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>logIn</servlet-name>
  <url-pattern>/logIn</url-pattern>
</servlet-mapping>


</web-app>

Upvotes: 0

Views: 6808

Answers (3)

wendy
wendy

Reputation: 1

not using localhost ,replace with ip eg: 'http://localhost:8080/apps/sessiondemo.html 'replace 'http://127.0.0.1:8080/apps/sessiondemo.html' and try again.

Upvotes: 0

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85789

Forget about scriptlets, avoid its usage. Use Expression language instead:

<div id="divUser">
    Welcome ${FirstName} ${LastName}
</div>

Here, ${FirstName} will be replaced by the attribute with name "FirstName" stored in one of these scopes:

  • Page Scope
  • Request Scope
  • Session Scope
  • Application Scope

This will only work when using it in JSP. HTML is static, so it's not compiled nor evaluated by the application server, and you will get no results.

More info:

You should also specify that you're using at least Servlets 2.4 to enable Expression Language. Currently, Servlet is in version 3.1, but you can use Servlets 3.0 in your Jetty 9+ installation. Change your web.xml file to have this statement at the top:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 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"
      version="3.0"> 

    <description>Test examples</description>
    <display-name>My Servlets</display-name>

    <!-- rest of the content of your web.xml -->

</web-app>

You can check the jetty servlet version compatibility here: Jetty Version Comparison Table

Upvotes: 1

kirti
kirti

Reputation: 4609

For session try using this...

<% request.getSession().getAttribute("FirstName") %>

Upvotes: 1

Related Questions