i have made a small web appl开发者_如何学JAVAication with
form.html output.jsp ServletOne.javaIn the form.html,users enters his name and chooses a icecream flavour
both the username and icecream flavour are set as session attributes these are displayed on output.jsp after processing by ServletOne.javai am facing three problems:
1.session.isNew() returns true ,i have used sesssion ,then why is it still a new session 2. i am not able to use else in the jsp,getting syntax error on token 3.there is a log out button on the output.jsp now i want that on clicking the log out button,the user should able to go out of sessioncan someone help
here are the files:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome</title>
</head>
<body>
<%String abc=(String)session.getAttribute("name"); %>
<%= "Welcome"+((String)session.getAttribute("textfield"))%></br>
<%= "You have choosen"+abc %>
<% if(session.isNew()) {%>
<% out.println("Wellllllllll"); %>
<%} %>
<% else {}%>
<form action="/ApplicationOne/Form.html" method="post" name="Logput">
<input name="Logout" type="submit" />
</form>
</body>
</html>
ServletOne.java
HttpSession session=req.getSession();
resp.setContentType("text/html");
PrintWriter pw=resp.getWriter();
String Icecream=req.getParameter("RadioGroup1");
session.setAttribute("name",Icecream );
session.setAttribute("textfield",req.getParameter("textfield"));
IceCream ob=(IceCream)getServletContext().getAttribute("icecream");
//pw.println("<html><body>");
//pw.println("Ice-Cream Flavour is"+ob.getFlavour());
//pw.println("Enjoy "+Icecream+"</html></body>");
session.setMaxInactiveInterval(1200);
RequestDispatcher view=req.getRequestDispatcher("/Output.jsp");
view.forward(req, resp);
form.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="/ApplicationOne/Welcome">
<p>
LoginID
<input type="text" name="textfield" />
<br/>
SELECT VALUE <br/>
<label>
<input type="radio" name="RadioGroup1" value="Vanilla" id="RadioGroup1_0" />
Vanilla</label>
<br />
<label>
<input type="radio" name="RadioGroup1" value="Chocolate" id="RadioGroup1_1" />
CHocholate</label>
<br />
<label>
<input type="radio" name="RadioGroup1" value="Strawberry" id="RadioGroup1_2" />
Strawberry</label>
<br />
<input type="submit" name="Submit" id="Submit" value="Submit" />
<br />
</p>
</form>
</body>
</html>
1.session.isNew() returns true ,i have used sesssion ,then why is it still a new session
Probably because the server container has just created a new session. Take a look at the request and response headers and at your browser's cookie cache. Is a session cookie being set? Does the session cookie keep changing?
2.i am not able to use else in the jsp,getting syntax error on token
Look at the Java code generated for the JSP and look for the syntax error there. The file will be some temporary directory in the web container directory tree.
3.there is a log out button on the output.jsp
now i want that on clicking the log out button,the user should able to go out of session
You will need to do this in a servlet. The simple way would be to call invalidate
on the Session object. This may or may not work, depending on how your login mechanism / session cookie management is implemented.
- session.isNew() returns true ,i have used session ,then why is it still a new session
Ans: Check if session tracking is happening properly. The possible issues could be , the session cookie header is not exchanged properly. Check if the session cookie header is sent along with the response header and the same is returned in the request header by the browser. to see the request/response headers use LiveHttpHeaders plugin for browser.
- i am not able to use else in the jsp,getting syntax error on token
Ans: Check the jsp, the ide should show the syntax error. You can also check the generated java file in the server setup.
- there is a log out button on the output.jsp
Ans: Invalidate the session.
精彩评论