balan
balan

Reputation: 39

How to use Session object in value attribute of `html` input field on jsp

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);
    }

Upvotes: 1

Views: 3511

Answers (1)

Dmytro Grynets
Dmytro Grynets

Reputation: 953

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

Upvotes: 2

Related Questions