#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    65
    Rep Power
    2

    How to use jsp/servlet?


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

    file path: WebContent > login.jsp
    Code:
           <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
    Code:
    ...
    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
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,023
    Rep Power
    1285
    You'll need to run a servlet container like Tomcat

IMN logo majestic logo threadwatch logo seochat tools logo