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

How to use jsp / servlet

59
I need help making proper way to make website using jsp/servlet.

file path: WebContent > login.jsp
Expand|Select|Wrap|Line Numbers
  1.        <form method="post" action="servlet">
  2.           what's your username?<br /> <input type="text" name="usernameF" /><br />
  3.           what's your password?<br /> <input type="password" name="passwordF" /><br />
  4.       <input type="submit" name="submitB" value="login" />
  5.        </form>
  6.  

file path: Java Resources > src > servlet.java
Expand|Select|Wrap|Line Numbers
  1. ...
  2. protected void doGet(HttpServletRequest request,
  3.             HttpServletResponse response) throws ServletException, IOException {
  4.         response.setContentType("text/html"); // useing html
  5.  
  6.         PrintWriter out = response.getWriter(); // write to browser
  7.         HttpSession session = request.getSession(true);
  8.  
  9.         if (request.getParameter("submitB") != null) {
  10.             if (usernameR.equals("")) {
  11.                 out.print("Error enter name");
  12.             } else if (passwordR.equals("")) {
  13.                 out.print("Error enter password");
  14.             } else {
  15.                 session.setAttribute("username", usernameR);
  16.                 response.sendRedirect(login.jsp);
  17.             }
  18.                 }
  19. }
  20. ...
  21.  


Question 1: is this the proper way of using jsp/servlet?
Question 2: when user hit submit button in html form than it will run java code. but in servlet.java class the error will never print on login.jsp. how can i print errors on login.jsp? by error i mean:
out.print("Error enter name");
out.print("Error enter password");

p.s i look tutorial online but they only use jsp which is not good coding. if you have proper jsp/servlet tutorial let me know.

Thank you so much
Dec 16 '13 #1
2 1636
chaarmann
785 Expert 512MB
Answert to question 1:
How do you define "proper"? If it is a small demo and security is not so important, then it is.

Answer to question 2: You should check the username/password fields (empty, invalid characters etc.) BEFORE you submit the request, by using Javascript in your browser. This puts load off your server. Then you can directly pop up the error message by using "alert('myErrorMessage')" and focus on the wrong field.
You must also check all input on the server AFTER submitting. Users can turn off javascript just before sending, or send a request by typing arguments directly into the URL, or by using hacking tools. Some errors (like wrong password) can only be checked on the server anyway. Then you should re-send the login page, enriched with an error message.

Tip to avoid a common beginner's mistake:
Never ever send an internal error message back to the client. Just log it, give it a number and then forward to a custom error page stating "There was an internal error logged under number nnnn. Please contact the administrator by providing this number under ...". I have seen so many pages where error like the following pop up that provides excellent info for easy hacking (just making one up, but you get the idea): "ASPX error in Oracle database table CUSTOMERS. Null cannot be compared to password 'MyPassword' for user 'John Doe'"
Dec 16 '13 #2
zmbd
5,501 Expert Mod 4TB
One question per thread please.
Both of your questions are quite broad... can you focus down to one particular issue you are having, provide the details, describe what you want, what's happening, and what have YOU done in an attempt to fix the issue.
Dec 25 '13 #3

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

Similar topics

1
by: Hai Tran | last post by:
Any help is appreciated. Installed Tomcat 4.1 and Mysql on a WinXP. I've manage to get Tomcat up and was able to view my first application ( myhome ) simple home page. Tomcat was installed under...
14
by: ramadeviirrigireddy | last post by:
Hi All, I have the following code for form and servlet. when the form is submitted the servlet will print the values passed by the form. i'm not getting the servlet o/p when i submit the...
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...
6
by: Sushmita | last post by:
hi all, I have wrriten code for a small web application. From my JSP page i am not able to call the servlet.. its throwing an exception. "javax.servlet.ServletException: Wrapper cannot find...
1
by: shiyamala | last post by:
Hi i am shiyam, i am having some problem in java, i am basic java programmer. i have to write one program is writing the data into textfile name "newfile.txt" from one servlet and read it from...
2
by: ManidipSengupta | last post by:
Hi, a few (3) questions for the Java experts, and let me know if this is the right forum. It deals with 100% java code (reason for posting here) but manages a Web browser with Javascript. Thanks in...
9
by: mjahabarsadiq | last post by:
Hi I have created a servlet that is to be started at the server startup. And I got it. In that I have created a object of another class and set it as a session attribute. What I am trying is...
2
by: jerald m | last post by:
Hi, For Login authentication. i create JSP Form. login.jsp(webapps/loginsystem/login.jsp) <form method="POST" action ="http://localhost:8085/loginsystem/lo" name="login"> <font...
0
by: enzo | last post by:
I want to create a maven scala spring hibernate project **using ECLIPSE**. Im following this tutorial : http://grahamhackingscala.blogspot.fr/2010/01/scala-spring-hibernate-maven-webapp-how.html ...
1
by: ajay13785 | last post by:
I want to insert data from Text box to oracle database ,so in my HTML where there are text box (and 3 Buttons-New ,Update and save)so when i click any of three buttons it takes me to redirectServlet...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.