Retrieval of form fields name at Server end

Purpose:

    How to retrieve names of fields, instead of values (from the form) at the Server Side?

Steps:
1) Create “MyServlet.java”, as servlet file (don’t know how to create a servlet file in Eclipse? Refer to our post: “ Servlet in Eclipse IDE“)

import java.io.*;
import java.util.Enumeration;
import javax.servlet.*;
import javax.servlet.http.*;
 
public class MyServlet extends HttpServlet
{
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		response.setContentType("text/html"); 
		// informing the client that which format of data/response will be send
		PrintWriter out= response.getWriter(); 
		
		Enumeration e1= request.getParameterNames();
		// will retrieve all text fields names, from the HTML form using "getParameterNames()" method
		// and store in Enumeration type "e1"
		//Enumeration is an interface, whose objects has capability to store in Array form
		
		out.print("<br> The names are <br>");
		
		while(e1.hasMoreElements()) // returns TRUE/FALSE, if value is present or not
		{
			out.print("<br>"+e1.nextElement()); 
			//returns the next value from "e1"
		}
	}
}

2) Create “index.html”, as HTML file (you can refer: “How to add HTML file in your Servlet Program using Eclipse“)

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form action="MyServlet" method="get">
Here are 4 fields, with 4 different names
1<input type="text" name="num"><br> 
2<input type="text" name="num1"><br> 
3<input type="text" name="num2"><br> 
4<input type="text" name="num3"><br> 
<input type="Submit" value="Submit">

</form>
</body>
</html>

3) Press “CTRL”+ F11
4

OUTPUT
1
2

Advertisements

Database connection in Servlet

Purpose:

    How to connect your Servlet with Database?

Steps:
1) Create “MyServlet.java”, as servlet file (don’t know how to create a servlet file in Eclipse? Refer to our post: “ Servlet in Eclipse IDE“)

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*; // for database purpose
 
public class MyServlet extends HttpServlet
{
	Connection con;
	Statement stmt;
	ResultSet rs;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		response.setContentType("text/html"); // informing the client that which format of data/response will be send
		PrintWriter out= response.getWriter(); 
		
		String name= request.getParameter("num");
		// will retrieve "NUM" field value entered by user using "getParameter()"
		// and store in STRING "name"
		
		try
		{
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			con= DriverManager.getConnection("Jdbc:Odbc:dbs123");
			stmt= con.createStatement();
			
			rs=stmt.executeQuery("Select * from Student where s_name= '"+name+"' ");
			// Retrieving all columns from Database where "name" matches
			// Name of table is "Student"
			
			if(rs.next())
			{
				out.println("<h3> Name=</h3>" +rs.getString("s_name")); // Printing Name from Database
				out.println("<br> Address=" +rs.getString("address"));  // Printing Address from Database
			}
			else
			{
				out.println("<h3> ID is not exists</h3>"); // If no such ID found in Database
			}
		}
		catch(Exception e)
		{
			out.println(e);
		}
	}

}

2) Create “index.html”, as HTML file (you can refer: “How to add HTML file in your Servlet Program using Eclipse“)

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form action="MyServlet" method="get">
Enter the ID of Student
<input type="text" name="num"><br> 

<input type="Submit" value="Submit">

</form>
</body>
</html>

3) Create “DBServlet” database file

Design View of database

4) Configure database in Administrative Tools from Control Panel
ODBC –> ADD –> Microsoft Access Driver (*.mdb,*.accdb)

Under “Data Source Name” enter: “dbs123” (as mentioned in Servlet file)
Click on “Select”
from “C” directory, choose appropriate database, i.e “DBServlet.accdb”
Click on “OK”

5) Press “CTRL”+ F11

OUTPUT

HTML + Servlet Program using Eclipse

Purpose:

    How to add a HTML file into your Servlet using Eclipse?

Steps:
1) Create the following Servlet file in Eclipse (don’t know how to create a servlet in Eclipse? Refer to our post: “ Servlet in Eclipse IDE“)

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class MyServlet extends HttpServlet
{
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		response.setContentType("text/html"); // informing the client that which format of data/response will be send
		PrintWriter out= response.getWriter(); 
		
		//All data entered into a "TEXT" field of form, is stored as "STRING"
		//Hence if we enter "1" in a "TEXT" field of form, it will be stored as "ONE"
		//To convert it to "INT", we use a predefined function called= "parseInt()"
		int a1= Integer.parseInt(request.getParameter("n1")); 
		// using "getParameter()" to retrieve data entered by user in "n1" field
		int a2= Integer.parseInt(request.getParameter("n2")); 
		
		if (request.getParameter("r1")!=null) // checking if 1st radio button checked or not?
		{
			out.println("<h3> Addition= </h3>"+(a1+a2));
		}
		else if(request.getParameter("r2")!=null) // checking if 2nd radio button checked or not?
		{
			out.println("<h3> Subtraction= </h3>"+(a1-a2));
		}
		else if(request.getParameter("r3")!=null) //checking if 3rd radio button checked or not?
		{
			out.println("<h3> Multiply= </h3>"+(a1*a2));
		}
		else
		{
			out.println("<h3> Divide= </h3>"+(a1/a2));
		}		
	}

}

2) Next create a HTML File
Right click on “WebContent” folder –> New –> HTML File

Enter File Name: “index.html” and click on “FINISH” button

3) Now replace the existing “index.html” file code with this

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form action="MyServlet" method="get">
Enter 1st Number <input type="text" name="n1"><br>
Enter 2nd Number <input type="text" name="n2"><br>

<input type="radio" name="r1" value="Add">Add<br>
<input type="radio" name="r2" value="Subtract">Subtract<br>
<input type="radio" name="r3" value="Multiply">Multiply<br>
<input type="radio" name="r4" value="Divide">Divide<br>
<input type="Submit" value="Submit">

</form>
</body>
</html>

4) Now press “CTRL”+F11, you will get the following screen. Click on “FINISH BUTTON”, selecting WEB SERVER.

5) You will get output

6) Enter two numbers and perform any option like: