Creating Java Servlet Web Application using Netbeans


Step1: Creating New Project
a) Goto File->New Project ( One window will open)
b) In Project Window , Select Web -> Web Application




c) Select Next
d) Enter the Name of the Project [Eg. MyServlet] and path
e) Select Next
f) Select the J2EE version and Server which you want to
deploy the web Application
[Eg : J2EE5 and Tomcat 6.1]
g) Finish the wizard.
Format of Servlet Project
MyServlet (Project Name)
|
|_Web Pages (create html,jsp files here)
|
|-Configuration Files (configure web.xml file)
|
|-Server Resources
|
|-Source Package (create java servlet here)
|
|_Test packages
|
|-Libraries (Add external jar file here)
|
|-Test Libraries
Step 2 : Creating JSP or HTML file
a) Right click on the Web Pages Folder
b) Select New -> HTML option from the pop up window.
c) Type the name of your html page.
d) Finish the wizard.
Eg. select.html
<form action="SelectDetails" method="get">Select the Branch
<select name="branch">
<option value="IT"> B.Tech - IT </option>
<option value="CSE"> B.Tech - CSE </option>
<option value="ECE"> B.Tech - ECE </option>
<option value="MECH"> B.Tech - Mech </option>
</select>
<input type="submit" value="GetDetails" />
</form>
Step 3: MySQL Database : Check Table Name : student
id    name   branch
1     Amar   IT
2     Amit    CSE
3     Antony   MECH
4     Anbu     ECE
103   Sree        MECH
102   Karthick  IT
104   Deepan   ECE
105   Moorthy   CSE
Step 4: Add MySQL Jconnector jar file under “Libraries” foleder
Libraries -> Right Click -> Select Add New Jar File / Folder
My Path : /media/Backup/mysql-connector-java-5.1.6-bin.jar
Step 5: Creating Servlet
a) Right click on the Source Package Folder
b) Select New -> Servlet option from the pop up window.
c) Type the name of your html page.
d) Finish the wizard.
Eg:SelectDetails.java
 
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class SelectDetails extends HttpServlet {
 
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Connection conn = null;
 
        try {
            String branch = request.getParameter("branch");
            String userName = "root";
            String password = "123456";
            String url = "jdbc:mysql://localhost/Check";
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection(url, userName, password);
            Statement s = conn.createStatement();
            s.executeQuery("SELECT id, name FROM student where branch = '" + branch + "'");
            ResultSet rs = s.getResultSet();
            while (rs.next()) {
                int idVal = rs.getInt("id");
                String nameVal = rs.getString("name");
 
                out.println("id = " + idVal + ", name = " + nameVal +"   ");
 
}
rs.close();
s.close();
 
} catch (Exception e) {
out.println(e.toString());
} finally {
out.close();
if (conn != null) {
                try {
                    conn.close();
                    out.println("Database connection terminated");
                } catch (Exception e) {
                }
            }
 
        }
    }
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
 
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
}
Step 4 : Run the project.
URL => http://localhost:8084/MyServlet/select.html
Output for CSE
id = 2, name = Amit
id = 105, name = Moorthy

No comments:

Post a Comment