473,394 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Login session problem

24
I am learning how to use servlet, I want create a session that when one logs in it will say "weclome 'username'" in the welcome page

the first code I wrote (first below), when i click submit it takes me to the next page, but I want to use session to create a msg that will say "welcome username" in that next page. so in the second code i wrote (second below), I am struggling to insert a session that will do that.

Expand|Select|Wrap|Line Numbers
  1. package TestWebApp;
  2.  
  3. import java.io.*;
  4. import java.net.*;
  5.  
  6. import javax.servlet.*;
  7. import javax.servlet.http.*;
  8.  
  9. /**
  10. *
  11. * @author Administrator
  12. * @version
  13. */
  14. public class RegLoginServlet extends HttpServlet {
  15.  
  16. /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
  17. * @param request servlet request
  18. * @param response servlet response
  19. */
  20. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  21. throws ServletException, IOException {
  22. response.setContentType("text/html;charset=UTF-8");
  23. PrintWriter out = response.getWriter();
  24.  
  25. out.println("<html>");
  26. out.println("<head>");
  27. out.println("<title>Servlet RegLoginServletHW2</title>");
  28. out.println("</head>");
  29. out.println("<body>");
  30. out.println("<form id=login_reg name=login_registration method=post action=CatServletHW2>");
  31. out.println("<table width=400 border=1 cellpadding=0 cellspacing=0>");
  32. out.println("<tr><td colspan=2 class=title bgcolor=339900>Log in / Register Account Infomation <td></tr>");
  33. out.println("<tr width=150 border=1 bgcolor=99ff66><td>Requested user name </td>" +
  34. "<td ><input type=text name=userName /></td></tr>");
  35. out.println("<tr width=150 border bgcolor=99ff66><td>Password </td>" +
  36. "<td ><input type=password name=password/></td></tr>");
  37. out.println("<tr><td colspan=2 class=title bgcolor=339900>For new user's account, confirm password </td></td></tr>");
  38. out.println("<tr width=150 border=1 bgcolor=99ff66><td>Confirm password </td>" +
  39. "<td class=r_table><input type=password name=password_Conf /></td></tr>");
  40. out.println("<tr><td colspan=2 bgcolor=339900 ><div align=center class=text_click>" +
  41. "<input type=submit name=Submit value=Submit />" +
  42. "<input type=reset name=Submit2 value=Reset />" +
  43. "</div></td></tr>");
  44. out.println("</table>");
  45. out.println("</body>");
  46. out.println("</html>");
  47.  
  48. out.close();
  49. }
  50. }
  51.  

***********************************************
second code

Expand|Select|Wrap|Line Numbers
  1. package TestWebApp;
  2.  
  3. import java.io.*;
  4. import javax.servlet.*;
  5. import javax.servlet.http.*;
  6. import java.util.*;
  7.  
  8. public class RegLoginServlet extends HttpServlet {
  9. public void doGet(HttpServletRequest request,
  10. HttpServletResponse response)
  11. throws ServletException, IOException {
  12. response.setContentType("text/html");
  13. HttpSession session = request.getSession();
  14. String heading;
  15. Integer accessCount =
  16. (Integer)session.getAttribute("accessCount");
  17. if (accessCount == null) {
  18. accessCount = new Integer(0);
  19. heading = "Welcome, Newcomer";
  20. } else {
  21. heading = "Welcome Back";
  22. accessCount = new Integer(accessCount.intValue() + 1);
  23. }
  24. out.println("<html>");
  25. out.println("<head>");
  26. out.println("<title>Servlet RegLoginServletHW2</title>");
  27. out.println("</head>");
  28. out.println("<body>");
  29. out.println("<form id=login_reg name=login_registration method=post action=CatServletHW2>");
  30. out.println("<table width=400 border=1 cellpadding=0 cellspacing=0>");
  31. out.println("<tr><td colspan=2 class=title bgcolor=339900>Log in / Register Account Infomation <td></tr>");
  32. out.println("<tr width=150 border=1 bgcolor=99ff66><td>Requested user name </td>" +
  33. "<td ><input type=text name=userName /></td></tr>");
  34. out.println("<tr width=150 border bgcolor=99ff66><td>Password </td>" +
  35. "<td ><input type=password name=password/></td></tr>");
  36. out.println("<tr><td colspan=2 class=title bgcolor=339900>For new user's account, confirm password </td></td></tr>");
  37. out.println("<tr width=150 border=1 bgcolor=99ff66><td>Confirm password </td>" +
  38. "<td class=r_table><input type=password name=password_Conf /></td></tr>");
  39. out.println("<tr><td colspan=2 bgcolor=339900 ><div align=center class=text_click>" +
  40. "<input type=submit name=Submit value=Submit />" +
  41. "<input type=reset name=Submit2 value=Reset />" +
  42. "</div></td></tr>");
  43. out.println("</table>");
  44. out.println("</body>");
  45. out.println("</html>");
  46.  
  47. out.close();
  48. }
  49.  
  50.  
  51. }
  52.  
Oct 6 '07 #1
3 1406
dmjpro
2,476 2GB
I think your code is right :-)
What error you are getting?

Debasis Jana
Oct 8 '07 #2
judge82
24
I think your code is right :-)
What error you are getting?

Debasis Jana
The first code is right but I want to have a message on top that will say "welcome 'john'" if the person logs in with a user name john
Oct 9 '07 #3
dmjpro
2,476 2GB
The first code is right but I want to have a message on top that will say "welcome 'john'" if the person logs in with a user name john
You can simply get it by using "HttpServletRequest.getParameter("user_name")" .
But if you want to check whether a particular "user" stays in "login session", still not logged off....for that case you should use "Cookie".

In the server side use "HttpServletRequest.getCookies()".
See "Cookie" class.


Debasis Jana
Oct 10 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Mike | last post by:
I can not figure out what is going on here. I hope somebody can please help!!! I've got an intranet ASP3 application running on a Win2k server. This application requires a login, so the user...
10
by: GreggTB | last post by:
I've got an page (LOGIN.ASPX) that receives the user's login information. During the page load, it checks the credentials against a database and, if validation is successful, creates an instance of...
2
by: Tom | last post by:
I hope someone can help me figure out what's going on here. I've re-read the section on sessions at php.net and Googled this high and low but I haven't found anything that quite explains my...
0
by: John Meyer | last post by:
index: <%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.OleDb" %> <!DOCTYPE html PUBLIC...
9
by: Ben | last post by:
Hello, I'll bet this has been asked a million times but I can't seem to find a thread that gives the clear example I need. This PC has MySQL and IIS configured and running. The MySQL database is...
8
tolkienarda
by: tolkienarda | last post by:
hi all i have a login script that is simplified with out any extra stuff. and it doesn't seem to work. i think the problem is something to do with session variables. but i am not sure what it is....
0
by: cbkprasad | last post by:
Hi, I have a critical problem regarding, avoiding the user to login to wibsite using the same credentials at the same time.The concept is once the user is logged in to application the that...
6
by: =?Utf-8?B?S2VsbHk=?= | last post by:
We just switched our web application from .NET 1.1 to 2. Once client can't login out of several that have been successful. They enter a correct user name and password, click the login button and...
3
by: satishknight | last post by:
Hi, Can some one tell me how to change the validation sequence for the code pasted below, actually what I want it when any one enters the wrong login information (already registered users) then it...
9
by: Josh | last post by:
I run a Joomla website and am familiar with php in some but not all aspects. Currently I am trying to find some solutions related to session handling. Am I correct in saying that "login" is kept...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.