java - Pass data to JSP page using spring -
i using spring(not mvc), servlet, jsp web app, wanted display list of users on jsp page, how can done? here code
loginservice.java
@service public class loginservice { @persistencecontext entitymanager em; public user getuserbyusername(string username, string password) { user user = null; try { user = em.createquery("from user u u.username = :username , u.password = :password", user.class) .setparameter("username", username) .setparameter("password", password) .getsingleresult(); } catch (exception e) { e.printstacktrace(); } return user; } public list<user> getlistofusers() { return em.createquery("from user u", user.class).getresultlist(); } }
loginservlet.java
@component public class loginservlet extends httpservlet { @autowired loginservice loginservice; @override public void init(servletconfig config) throws servletexception { super.init(config); springbeanautowiringsupport.processinjectionbasedoncurrentcontext(this); } protected void processrequest(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { entitymanagerfactory emf = (entitymanagerfactory) request.getservletcontext().getattribute("emf"); string username = request.getparameter("username"); string password = request.getparameter("password"); user user = loginservice.getuserbyusername(username, password); if(user != null){ request.getsession().setattribute("user", user); response.sendredirect("home.jsp"); } else{ response.sendredirect("login.jsp"); } } @override protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { processrequest(request, response); } @override protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { processrequest(request, response); } }
login.jsp
<%@page contenttype="text/html" pageencoding="utf-8" %> <!doctype html> <html> <head> <title>login page</title> </head> <body> <form id="form" name="form" method="post" action="login"> <h1>login</h1> please enter login information <br/>new user? <a href="register.jsp">register</a> enter user id <input type="text" name="username" id="userid"/> password <input type="password" name="password" id="password"/> <button type="submit">sign-in</button> </form> </body> </html>
home.jsp
<%@page import="demo.spring.entity.user" %> <%@page import="java.util.date" %> <%@ page import="java.util.list" %> <%@page contenttype="text/html" pageencoding="utf-8" %> <!doctype html> <html> <head> <title>home page</title> </head> <body> <h1>user</h1> <form id="form" name="form" method="post" action="update"> <p> <%=new date()%></br> <% user user = (user) session.getattribute("user"); %> <b>welcome <%= user.getfirstname() + " " + user.getlastname()%> </b> <br/> <a href="logout.jsp">logout</a> </p> <table> <thead> <tr> <th>user id</th> <th>first name</th> <th>middle name</th> <th>last name</th> <th>email</th> </tr> </thead> <%--<tbody> <% loginservice loginservice = new loginservice(); list<user> list = loginservice.getlistofusers(); (user u : list) { %> <tr> <td><input type="text" name="firstname" id="firstname" value="<%=u.getusername()%>"/></td> <td><%=u.getfirstname()%></td> <td><%=u.getmiddlename()%></td> <td><%=u.getlastname()%></td> <td><%=u.getemail()%></td> </tr> <%}%> <tbody>--%> </table> <br/> </form> </body> </html>
please tell me correct way? , please suggest if code correct or modification needed in terms of design or correct practice?
previously used create loginservice object in home.jsp not right way, need autowire service in jsp, or rather think if pass data view layer rather fetching in view layer?
java code in jsp pages considered bad practice. using spring, should create use controllers interact other components, , add information displayed model passed jsp rendering. see spring mvc reference documentation detailed information.
Comments
Post a Comment