[英]Is it advisable to call java dao class method from a jsp page
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 我希望用户单击查看按钮后可以看到他/她自己的详细信息。因此,在一个jsp页面中,我创建了一个这样的链接
<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. 我在身份验证时已将empid设置为session属性,但现在将用户重定向到以下链接,其中我仅需显示其详细信息。
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. 我的疑问是,我们显然可以调用dao类方法来从jsp页面获取用户数据,但这是一种好的编程习惯,还是我必须先调用servlet,然后才调用jsp。
You must not have any scriptlet/direct Java code in your JSP. 您的JSP中不得包含任何scriptlet /直接Java代码。 Please avoid its usage .
请避免使用它 。 More info on this: How to avoid Java code in JSP files?
有关更多信息: 如何避免JSP文件中的Java代码? (no need to explain one more time all the problems you get when using scriptlets).
(无需再解释一次使用scriptlet时遇到的所有问题)。
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). 您应该使用一个Servlet,它将处理JSP的GET请求,而Servlet(控制器)将成为您的View与模型(Service,Dao等)之间的链接。 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): 然后在您的viewuser.jsp文件中(缩短的代码):
<p>
Name: <c:out value="${employee.name}" />
</p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.