I am new to JSP. so begin with, I decided to go slow. Right now I am just trying to get user input and print it back using Netbean.

Here is the code which I made. Can you please tell me why this is not printing?

package newpackage;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class ReturnName extends HttpServlet {
   
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ReturnName</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet ReturnName at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
        PrintWriter out = response.getWriter();
        String getName = request.getParameter("name");
        
        out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ReturnName</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println(getName);
            out.println("</body>");
            out.println("</html>");
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        doGet(request, response);
        
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

Code for the form:

<form action="EnterName" method="POST" enctype="multipart/form-data" >
  Enter your name:<INPUT TYPE="TEXT" NAME="name">
      <input type="submit">
</form>

Thank you.

This is a servlet, not a JSP, and this sort of thing

out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ReturnName</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println(getName);
            out.println("</body>");
            out.println("</html>");

is frowned upon these days in a Servlet.

See http://java.sun.com/javaee/5/docs/tutorial/doc/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.