473,699 Members | 2,417 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Login session problem

24 New Member
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 1414
dmjpro
2,476 Top Contributor
I think your code is right :-)
What error you are getting?

Debasis Jana
Oct 8 '07 #2
judge82
24 New Member
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 Top Contributor
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 "HttpServletReq uest.getParamet er("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 "HttpServletReq uest.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
1787
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 must first go to the login.asp page before accessing any other .asp page. I have set up a redirection method so that if a user pastes a URL to an internal .asp page, I store the URL in a session variable, then I direct them to the login page. After...
10
12201
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 an object that stores the user's basic profile data (username, user type, associated sales region, etc.). I've been taking this user info and placing it in the Session object like so... Session = user;
2
2054
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 problem. The basic problem: session data (e.g. $_SESSION) gets dropped after visitor logs in and reloads the page via a form once or twice (it seems to vary), requiring the visitor to re-login, whereupon the problem repeats itself. An outline of...
0
1139
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 "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">
9
3591
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 "myDB" with a table "myUsers" with fields "Username" and "Password". I also have the MySQL ODBC driver loaded with a DSN "dsnMySQL" setup. First question is can someone direct me to a site or provide a sample code for a login page that...
8
2139
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. below are the scripts in their entirety along with a description of all outputs first page - login.htm <html> <head> <title>login</title> </head> <body> <form action="login.php" method="post">
0
3251
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 user credentials should be locked, i.e. no others should be able to login with that credentials.... What I have tried is with the following code In web.Config I added following code: <sessionState mode="StateServer"...
6
2278
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 they are redirected back to login with no errors or login failure. The same login account works fine from other machines at different locations. This client had no problem logging in before we switched. The login procedures are essentially the...
3
6221
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 has to tell then them its wrong information but currently it takes then to a next page and then tells them its incorrect information. This is tedious as every time they enter wrong they will be redirected to a different page and then they have to...
9
7806
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 in sessions? I can see active sessions in my mysql database, but is that the only place this information is stored? Sessions and cookies I know are related also, but how specifically (session info stored in cookies?)? Right now, when users...
0
8613
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8908
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7745
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.