473,698 Members | 2,943 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

I am having problems creating login session with java Servlet.

24 New Member
please I am learning how to use servlet. here is my 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.  
  53.  
  54.  
Sep 26 '07 #1
7 2207
r035198x
13,262 MVP
... and what is the question?
Sep 26 '07 #2
judge82
24 New Member
I wan create a session that when one logs in it will say "weclome 'username'" in the welcome page
Sep 26 '07 #3
r035198x
13,262 MVP
I wan create a session that when one logs in it will say "weclome 'username'" in the welcome page
You have to pardon my extreme ignorance but my pyschic powers elude me when I don't have Rooibos tea.

So where are you specifically stuck with? Did you try your code and it gave you errors/exceptions/wrong results?
Sep 26 '07 #4
judge82
24 New Member
ok. the first code I wrote (below), when you click submit it takes you to the next page, but I want to have a session that can say "welcome username" in that new page. so in the first code i posted (above), 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.     }
Sep 26 '07 #5
r035198x
13,262 MVP
You have gone through a tutorial on sessions?
Sep 26 '07 #6
judge82
24 New Member
You have gone through a tutorial on sessions?
no. do you know where I can get a good one?
Sep 26 '07 #7
r035198x
13,262 MVP
no. do you know where I can get a good one?
You know what? You don't need to manually create a session for a log in process. Just create your log in HTML or JSP page and submit that to the servlet. A session is automatically created for you then.

P.S I don't know a good sessions tutorial but you should have Sun's J2EE tutorial. It's available free for download.
Sep 27 '07 #8

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

Similar topics

1
3473
by: Chris Morgan | last post by:
I'm trying to get php to run on my webserver as a Java Servlet, it works the first time but fails the second time and crashes the JVM with the following error: I have tried the latest versions of PHP 4. (release and dev) I would appreciate any help with this issue. -- Chris
0
4489
by: JShurmatz | last post by:
If anyone can shed some light on this problem I would greatly appreciate it. I am unsuccessfully trying to use a database connnection retrieved from a pool configured using Java System Web Server 6.1 with the SQL Server 2000 JDBC Driver. The background: 1. I have downloaded the SQL Server 2000 JDBC Driver and installed it on the web server.
3
3177
by: Alan Krueger | last post by:
Greetings, I've been able to cache Transformer objects in a Tomcat-based servlet application to avoid unnecessary Transformer rebuilding, except for certain ones on certain machines. I'm running Tomcat 4.1.27 under Eclipse 2.1.0 using the Sysdeo Tomcat plugin using j2re1.4.1_02 under Windows 2000 SP4. I've digested this down to a small (albeit convoluted) sample that exhibits the behavior reliably on my development workstation.
0
1692
by: Per-Christian Engdal | last post by:
Hi, I have built a cocoon.war file, and deployed it on my BEA Weblogic 8.1 Sp2 (Windows 2000) installation. The deployment works without exceptions, but when I try to access cocoon through http://localhost:80/cocoon I get the following error message (stack trace): java.lang.NoClassDefFoundError: org/apache/log/Logger at org.apache.avalon.framework.logger.LogKitLogger.isWarnEnabled(LogKitLogger.java:122)
2
3977
by: astolpho | last post by:
I am using a slightly outdated reference book on J2EE programming. It gives 2 methods of creating a database used in its casestudies. The first is an ANT script that gives the following output: D:\original\CaseStudy-2-5\CaseStudy\Day02\exercise>asant database Buildfile: build.xml env-user: prop-user: set-user:
3
1414
by: judge82 | last post by:
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. package TestWebApp; ...
7
1766
evilmonkey
by: evilmonkey | last post by:
My assignment was to create a shopping cart servlet, it works locally on tomcat but when I deploy it to the schools server (also tomcat) it fails to refresh the front page and just prints the HTML Code not the page. Any thoughts or a helping hand would be appreciated. import java.io.*; import java.net.*; import java.text.NumberFormat; import java.util.*; import javax.servlet.*; import javax.servlet.http.*;
0
2645
by: krishna81m | last post by:
Could some one please explain why the session is not being maintained when I am doing a forward in a servlet after setting a cookie. I am even unable to set session attributes or parameters and do a forward from a servlet to another servlet via a JSP page, the attributes seize to exist. I still do not understand the concept of session tracking. First page JSP: _______________________________________________ <%@ page...
5
7064
by: sayeo87 | last post by:
Hi, I am quite new to JSP so please forgive me if I ask really simple things... I am trying to run system commands on the server and display the output on a webpage. This is what I've got: <%@ page import="java.io.*" %> <HTML> <BODY> <% Runtime rt = Runtime.getRuntime(); Process p = rt.exec("/bin/ls");
0
8610
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
9170
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
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...
0
8873
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5862
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
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3052
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
2339
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
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.