0

I want the user to see his/her own details after clicking the view button.So in a jsp page i have created a link like this

<a href="viewuser.jsp">View</a>

I have set the empid as session attribute earlier while authenticating but now as i am redirecting the user to the following link where i have to show his/her details only.

My doubt is we can obviously call the dao class method to fetch the user's data ,from the jsp page but is it a good programming practice or i have to call the servlet first then jsp.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Mayank
  • 25
  • 5

1 Answers1

1

You must not have any scriptlet/direct Java code in your JSP. Please avoid its usage. More info on this: How to avoid Java code in JSP files? (no need to explain one more time all the problems you get when using scriptlets).

You should use a Servlet that will handle the GET request for your JSP and the Servlet (Controller) will be the link between your View and your Model (Service, Dao, etc). This is a very basic example:

@WebServlet("/viewuser.jsp")
public class HelloServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        int empid = (Integer)request.getSession().getAttribute("empid");
        //assuming EmployeeService is a service class that has a getEmployee method
        //that will handle the work to a dao to retrieve data from database 
        //or another data source and will return an Employee object
        Employee employee = new EmployeeService().getEmployee(empid);
        request.setAttribute("employee", employee);
        request.getRequestDispatcher("/viewuser.jsp").forward(request, response);
    }
}

And then in your viewuser.jsp file (shortened code):

<p>
    Name: <c:out value="${employee.name}" />
</p>
Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332