Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: how to use jsp / servlet

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default how to use jsp / servlet

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

    file path: WebContent > login.jsp
           <form method="post" action="servlet">
              what's your username?<br /> <input type="text" name="usernameF" /><br />
              what's your password?<br /> <input type="password" name="passwordF" /><br />
    	  <input type="submit" name="submitB" value="login" />
           </form>


    file path: Java Resources > src > servlet.java
    ...
    protected void doGet(HttpServletRequest request,
    			HttpServletResponse response) throws ServletException, IOException {
    		response.setContentType("text/html"); // useing html
     
    		PrintWriter out = response.getWriter(); // write to browser
    		HttpSession session = request.getSession(true);
     
    		if (request.getParameter("submitB") != null) {
    			if (usernameR.equals("")) {
    				out.print("Error enter name");
    			} else if (passwordR.equals("")) {
    				out.print("Error enter password");
    			} else {
    				session.setAttribute("username", usernameR);
    				response.sendRedirect(login.jsp);
    			}
                    }
    }
    ...



    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


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: how to use jsp / servlet

    Quote Originally Posted by game06 View Post
    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.
    First, Servlets it's better if they are in a package, not in the "default" package.
    Second, you must map, into the web.xml, the url /servlet (or whatever url you would like to specify in the action attribute) to your servlet. It's not automatic. Putting action="servlet" doesn't mean, automatically, that a servlet named "servlet" is invoked!
    Third, in the "good" use of JSP/Servlet, a Servlet should not do output directly to the response. At most it should do a redirect or a forward to another internal resource (e.g. a JSP, tipically).
    Fourth (not less important), since the form is submitted in POST, your servlet must handle this request in the doPost, not doGet.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

Similar Threads

  1. Jsp Servlet problem with getting parameter vaules from another jsp page
    By venkat_tk in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: February 25th, 2013, 02:51 AM
  2. JSP and Servlet Project
    By GoodGirl in forum Member Introductions
    Replies: 3
    Last Post: December 14th, 2011, 07:49 AM
  3. Need help with a simple jsp and servlet problem
    By javacoder_APAC in forum Java Servlet
    Replies: 4
    Last Post: March 23rd, 2011, 08:32 AM
  4. jsp- servlet problems
    By kundan_101 in forum Java Servlet
    Replies: 1
    Last Post: December 31st, 2010, 05:42 PM
  5. Servlet or Jsp?
    By flangofas in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: December 8th, 2010, 11:54 AM