1

I am using rollno as session object and i have to use that value in the value attribute of "html" input field here is my jsp coding

     <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
     <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     <%@page import="javax.servlet.http.HttpSession"%> 
     <html>
     <head>
     <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
     <title>Insert title here</title>
     <%!HttpSession value=null; %>
     </head>
     <body>
     <form action="ShowMarkServlet">
     <% value=(HttpSession)session.getAttribute("rollno");%>
     <%out.print(session.getAttribute("rollno")); %>
     Rollnumber:<input type="number" value="<%=value%>" name="rollno"><br>
     Enter the semester:<input type="number" name="semester" min="0" max="6">
     <input type="submit" value="okay">
     </form>
     </body>
     </html>

Here is my servlet coding the rollno has value in this servlet but shows null in the jsp file.

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

      String rollno=request.getParameter("rollnumber");
      System.out.println(rollno);
      String report=request.getParameter("domainarea");
      System.out.println(report);
      HttpSession session=request.getSession();
      session.setAttribute(rollno,"rollno");
      if(report.equals("MarkDetail")){
        request.getRequestDispatcher("/sem.jsp").forward(request, response);
    }
2
  • It depends on what are you going to do with it, understand that those htmls are formed by servlet in which jsp transforms after compilation Commented Feb 20, 2017 at 15:45
  • okay but how to access that session object ,it shows null in the jsp display <%out.print(session.getAttribute("rollno")); %> Commented Feb 20, 2017 at 16:03

1 Answer 1

2

You can access session from jsp just by session, so your code should look like:

<% String value=session.getAttribute("rollno");%>
<% out.print(value); %>

Or using EL:

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 <title>Insert title here</title>
 </head>
 <body>
 <form action="ShowMarkServlet">
 Rollnumber:<input type="number" value="${rollno}" name="rollno"><br>
 Enter the semester:<input type="number" name="semester" min="0" max="6">
 <input type="submit" value="okay">
 </form>
 </body>
 </html>

If i got your intensions right

Sign up to request clarification or add additional context in comments.

3 Comments

Session is just there, holding values you've written in there, you should not getting it anyway harder than using session in jsp, and in EL all variables are looked through request and session so you should not even bother about where they are
My intention is same but its too not working ${rollno}session.setAttribute(String arg,"obj") we can not save an object in a String <% String value=session.getAttribute("rollno");%>`
I just wrote string following your example, any object can be stored in session, and then you should access it as object, for example you do: request.getSession().setAttribute("rollno", a) in your servlet where a is an object of A, then inside jsp you should do A a = (A)session.getAttribute("rollno") and the you can use it as A object, accessing it's fields and methods

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.