Home / java / First Servlet Program

First Servlet Program

How to write and execute your first Servlet Program in Eclipse Editor.

Create Dynamic Web Project: While creating the Dynamic web Project you have to choose 2.5 Dynamic web module version

DWA 2.5
Servlet

Application Data Source :

package com.testsevlet;

import java.sql.Connection;
import org.apache.commons.dbcp2.BasicDataSource;

public class ApplicaionDataSource {

	static BasicDataSource data=null;
	
	static {
		data=new BasicDataSource(); 
		data.setUsername("root");
		data.setPassword("");
		data.setUrl("jdbc:mysql://localhost/jdbc");
		data.setDriverClassName("com.mysql.jdbc.Driver");
		
	}
	/*ApplicaionDataSource.getConnection()*/
	 public static Connection getConnection() throws Exception{
		return data.getConnection();
	 }
	
}

Servelt :


package com.testsevlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet{
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//super.doPost(req, resp);
		PrintWriter pw = resp.getWriter();
		pw.write("Test Servlet");
		System.out.println("Control inside TestServlet");
		
		try{
			Connection conn  = ApplicaionDataSource.getConnection();		
			java.sql.PreparedStatement ps = conn.prepareStatement("select * from f_user");
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				pw.write(rs.getString("name"));
				pw.write("<br />");
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}

Run a Servlet Application with IDE Eclipse:

How to clean the Cache of Application before Run
1. Stop the Server
2. Clear Tomcat Work Directory: Right click on servlet you can see the option “Clear Tomcat Work Directory”
3: Clean: Right click on the Mouse you can see “Clean”
4. Project Clear: In IDE click on Project Menu you can see “Clean”.

Make a controller as Landing Page in web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>spring</display-name>
  <welcome-file-list>
  <welcome-file>test</welcome-file>
  </welcome-file-list>
  
  <servlet>
  <servlet-name>Test</servlet-name>
  <servlet-class>com.testsevlet.TestServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
  <servlet-name>Test</servlet-name>
  <url-pattern>/test</url-pattern>
  </servlet-mapping>
</web-app>

About Mohammad Fareed

Software Engineer @ Tekzenit.

Check Also

Object Creation in Java

1)How many ways are there to create an object? 1. How many ways to create …

Leave a Reply

Your email address will not be published. Required fields are marked *