
I am learning Java Web, but I have some problems and I need help. I use template.jsp, which I include header.jsp, footer.jsp, login.jsp (the left side of the template) and ${param.content}.jsp . For each page named X.jsp, I made another jsp with the following contents, because I want each page to have the same layout:
<jsp:include page="template.jsp"> <jsp:param name="content" value="X"/> <jsp:param name="title" value="Start navigate"/>`enter code here` </jsp:include>
When I click on the Browse link, for example, I want to redirect to Review.jsp, but I have some problems. In footer.jsp, I have something like this:
(...) < a href =" Review.jsp "> Review </a> (...)
After logging in, I try to click Browse, it will send me to Review.jsp, but it shows me that I have not logged in. I am using Spring Framework, Tomcat 7, Eclipse, MySQL. When I log in, I create a cookie:
String timestamp = new Date().toString(); String sessionId = DigestUtils.md5Hex(timestamp); db.addSession(sessionId, username, timestamp); Cookie sid = new Cookie("sid", sessionId); response.addCookie(sid);
For each method, I have something like this (I saw this in the tutorial):
@RequestMapping(value = "/writeReview", method = RequestMethod.GET) public String nameMethod(@CookieValue("sid") String sid,...)
Using sid, I can find out who the user is. I have a database with a user account and some other tables. The problem is that when I click on a review or whatever, it shows me that I am not registered. What can I do?
UPDATE :
I am using JavaBean for user and login.jsp. JSP, which has a text box for login, I have a GET method when I press the button for login.
@RequestMapping(value = "/login", method = RequestMethod.GET) public ModelAndView createCookie( @RequestParam("username") String username, @RequestParam("password") String password, HttpServletRequest request, HttpServletResponse response) throws SQLException { //code for creating the cookie here, after testing if the user is in my database }
For each page, I submit a sid, and I have an attribute for the model, username:
public String methodName(@CookieValue(required = false) String sid, Model model) { if (sid != null) { User user = getUserBySid(sid); model.addAttribute("user", user); } (..other data.) return "nameJSP.jsp"; }
I check every JSP if the username is not empty, so that I can see that the user is logged in. The application is going well, it passes parameters if I donβt click the links from the header or footer. The problem is that I have to pass, say, a JSP parameter which is the actual layout content for the JSP that the footer refers to, and that JSP will be the next content of my layout. The layout recognizes only the content and title:
<title>${param.title}</title> (I didn't paste all the code, I use a table <table>....) <%@ include file="header.jsp"%> <%@ include file="login.jsp"%> <jsp:include page="${param.content}.jsp"/> <%@ include file="footer.jsp"%>
So, how can I include a parameter in this JSP to be received from another JSP? Also, should layout.jsp be accessed and sent to the footer or header?
<jsp:include page="layout.jsp"> <jsp:param name="content" value="X"/> <jsp:param name="title" value="Start navigate"/> </jsp:include>