473,385 Members | 1,814 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,385 software developers and data experts.

I am having problems creating login session with java Servlet.

24
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 2192
r035198x
13,262 8TB
... and what is the question?
Sep 26 '07 #2
judge82
24
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 8TB
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
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 8TB
You have gone through a tutorial on sessions?
Sep 26 '07 #6
judge82
24
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 8TB
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
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...
0
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...
3
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...
0
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...
2
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: ...
3
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...
7
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...
0
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...
5
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: <%@...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.