Servlet on NetBeans IDE:

Let’s practice the NetBeans IDE 6.1 with following steps to create a new Servlet Web project. In this application Tomcat 6 is used as the server.

Step 1: select File->New Project, and Web->Web Application.

Step 2:Next, specify the project name and project location in of “Name and Location”

Step 3: Skip Framework selections for the time being.

Step 4:Now let’s create a simple Servlet Application. Right click on the Project file and create a new Servlet file.

Give a any Class Name and a package of your interest. Here its HelloUser and the package Name given is User.

Step4:Copy the following code:

Code1: HelloUser.java

package User;

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class HelloUser extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();

try {

out.println("<html>");

out.println("<head>");

out.println("<title>Servlet HelloUser</title>");

out.println("</head>");

out.println("<body>");

out.println("<h3>HelloUser Servlet</h3>");

out.println("<form method=\"post\">");

out.println("User Name: Kai, Password : qian </br</br>");

out.println("Enter Name:<input type=\"text\" name=\"username\">");

out.println("<input type=\"password\" name=\"password\">");

out.println("<input type =\"submit\" value=\"Submit\">");

out.println("</form>");

if (request.getParameter("username").equalsIgnoreCase("Kai") & request.getParameter("password").equalsIgnoreCase("qian")) {

RequestDispatcher rd;

rd = request.getRequestDispatcher("/Success");

rd.forward(request, response);

if (rd != null) {

rd.include(request, response);

}

}

out.println("</body>");

out.println("</html>");

} finally {

out.close();

}

}

@Override

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

processRequest(request, response);

}

@Override

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

processRequest(request, response);

}

@Override

public String getServletInfo() {

return "Short description";

}

}

Now Create a similar Servlet file called Success and copy this code:

Code2:Success.java

package User;

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class Success extends HttpServlet {

@Override

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();

out.println("Hello! <h3>"+request.getParameter("username")+" </h3>You have Successfully Loged In");

out.close();

}

}

Step 5:

To run the Servlet file, Right click on the HelloUser file which is located at the Source Package below the User package and click Run file.

The NetBeans will run Tomcat server and you can use browser to visit localhost to access this Servlet file:

If you type in User Name as Kai and password as qian, it activates the Success Servlet file and the output is as follows: