0% found this document useful (0 votes)
5 views28 pages

Understanding Java Servlets Basics

Java Servlets are programs that act as a middle layer between web clients and servers, enabling dynamic web page creation and data processing. They operate within a specific life cycle consisting of initialization, request handling, and destruction, with key methods including init(), service(), doGet(), and doPost(). The document also covers servlet packages, server setup using Tomcat, and examples of handling form data through GET and POST methods.

Uploaded by

melesew mossie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views28 pages

Understanding Java Servlets Basics

Java Servlets are programs that act as a middle layer between web clients and servers, enabling dynamic web page creation and data processing. They operate within a specific life cycle consisting of initialization, request handling, and destruction, with key methods including init(), service(), doGet(), and doPost(). The document also covers servlet packages, server setup using Tomcat, and examples of handling form data through GET and POST methods.

Uploaded by

melesew mossie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introduction to Servlets

What are Servlets?


Java Servlets are programs that run on a Web or Application server and act as a middle layer between
requests coming from a Web browser or other HTTP client and databases or applications on the HTTP
server.

Using Servlets, you can collect input from users through web page forms, present records to a database or
another source, and create web pages dynamically.

Servlets Architecture:
Following diagram shows the position of Servelts in a Web Application.

Servlets Tasks:
Servlets perform the following major tasks:

• Read the explicit data sent by the clients (browsers). This includes an HTML form on a Web page
or it could also come from an Applet or a custom HTTP client program.
• Read the implicit HTTP request data sent by the clients (browsers). This includes cookies, media
types and compression schemes the browser understands, and so forth.
• Process the data and generate the results. This process may require talking to a database, invoking
a Web service, or computing the response directly.
• Send the explicit data (i.e., the document) to the clients (browsers). This document can be sent in
a variety of formats, including text (HTML or XML), GIF images, Excel, etc.

1
• Send the implicit HTTP response to the clients (browsers). This includes telling the browsers or
other clients what type of document is being returned (e.g., HTML), setting cookies and caching
parameters, and other such tasks.

Servlets Packages:
Java Servlets are Java classes run by a web server that has an interpreter that supports the Java Servlet
specification.

Servlets can be created using the [Link] and [Link] packages, which are a standard part
of the Java's enterprise edition, an expanded version of the Java class library that supports large-scale
development projects.

These classes implement the Java Servlet and JSP specifications. Java servlets have been created and
compiled just like any other Java class. After you install the servlet packages and add them to your
computer's Classpath, you can compile servlets with the JDK's Java compiler or any other current
compiler.

Setting up Web Server: Tomcat


A number of Web Servers that support servlets are available in the market. Some web servers are freely
downloadable and Tomcat is one of them.

Apache Tomcat is an open source software implementation of the Java Servlet and JavaServer Pages
technologies and can act as a standalone server for testing servlets and can be integrated with the Apache
Web Server.

Servlets - Life Cycle

A servlet life cycle can be defined as the entire process from its creation till the destruction. The following
are the paths followed by a servlet

• The servlet is initialized by calling the init () method.


• The servlet calls service() method to process a client's request.
• The servlet is terminated by calling the destroy() method.

2
• Finally, servlet is garbage collected by the garbage collector of the JVM.

Now let us discuss the life cycle methods in details.

The init( ) method :


The init method is designed to be called only once. It is called when the servlet is first created, and not
called again for each user request. So, it is used for one-time initializations, just as with the init method of
applets.

The servlet is normally created when a user first invokes a URL corresponding to the servlet, but you can
also specify that the servlet be loaded when the server is first started.

When a user invokes a servlet, a single instance of each servlet gets created, with each user request
resulting in a new thread that is handed off to doGet or doPost as appropriate. The init() method simply
creates or loads some data that will be used throughout the life of the servlet.

The init method definition looks like this:

public void init() throws ServletException {


// Initialization code...
}

The service() method :


The service() method is the main method to perform the actual task. The servlet container (i.e. web server)
calls the service() method to handle requests coming from the client( browsers) and to write the formatted
response back to the client.

Each time the server receives a request for a servlet, the server spawns/generates a new thread and calls
service. The service() method checks the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls
doGet, doPost, doPut, doDelete, etc. methods as appropriate.

Here is the signature of this method:

public void service(ServletRequest request,ServletResponse response)


throws ServletException, IOException{
}

3
The service ( ) method is called by the container and service method invokes doGet, doPost, doPut,
doDelete, etc. methods as appropriate. So you have nothing to do with service ( ) method but you override
either doGet() or doPost() depending on what type of request you receive from the client.

The doGet() and doPost() are most frequently used methods within each service request. Here is the
signature of these two methods.

The doGet() Method


A GET request results from a normal request for a URL or from an HTML form that has no METHOD
specified and it should be handled by doGet() method.

public void doGet(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
// Servlet code

The doPost() Method


A POST request results from an HTML form that specifically lists POST as the METHOD and it should
be handled by doPost() method.

public void doPost(HttpServletRequest request,


HttpServletResponse response)
throws ServletException, IOException {
// Servlet code
}

The destroy() method :


The destroy() method is called only once at the end of the life cycle of a servlet. This method gives your
servlet a chance to close database connections, halt background threads, write cookie lists or hit counts to
disk, and perform other such cleanup activities.

After the destroy() method is called, the servlet object is marked for garbage collection. The destroy
method definition looks like this:

public void destroy() {


// Finalization code...
}

4
Architecture Digram:
The following figure depicts a typical servlet life-cycle scenario.

• First the HTTP requests coming to the server are delegated to the servlet container.
• The servlet container loads the servlet before invoking the service( ) method.
• Then the servlet container handles multiple requests by spawning multiple threads, each thread
executing the service( ) method of a single instance of the servlet.

Servlets - Examples

Servlets are Java classes which service HTTP requests and implement the [Link] interface.
Web application developers typically write servlets that extend [Link], an abstract
class that implements the Servlet interface and is specially designed to handle HTTP requests.

5
Sample Code for Hello World:
Following is the sample source code structure of a servlet example to write Hello World:

// Import required java libraries


import [Link].*;
import [Link].*;
import [Link].*;

// Extend HttpServlet class


public class HelloWorld extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse


response)throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
PrintWriter out = [Link]();
try {
// TODO output your page here
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Welcome to java servlet</title>");
[Link]("</head>");
[Link]("<body>");

[Link]("<h1> This is my first java servlet program</h1>");

[Link]("<a href=\"[Link]
[Link]("</body>");
[Link]("</html>");

} finally {
[Link]();
}
} }

6
Servlets Form Data/query data

Before looking into how a servlet can read data submitted via a form, it is necessary to understand how
data from an HTML form is passed to the web server by the browser.

There are two common ways of passing data from the web browser to the web server. They are called
POST and GET methods. Both of these methods pass the data in a 'key=value' format. The key for each
data field is specified as part of the tag describing the relevant field.

GET method:

The GET method sends the encoded user information appended to the page request. The page and the
encoded information are separated by the ? character as follows:

[Link]

The GET method is the default method to pass information from browser to web server and it produces
a long string that appears in your browser's Location: box. Never use the GET method if you have
password or other sensitive information to pass to the server. The GET method has size limitation: only
1024 characters can be in a request string.

This information is passed using QUERY_STRING header and will be accessible through
QUERY_STRING environment variable and Servlet handles this type of requests using doGet() method.

POST method:
A generally more reliable method of passing information to a backend program is the POST method. This
packages the information in exactly the same way as GET methods, but instead of sending it as a text
string after a ? in the URL it sends it as a separate message. This message comes to the backend program
in the form of the standard input which you can parse and use for your processing. Servlet handles this
type of requests using doPost() method.

7
Reading Form Data using Servlet:
Servlets handles form data parsing automatically using the following methods depending on the situation:

• getParameter(): You call [Link]() method to get the value of a form parameter.
• getParameterValues(): Call this method if the parameter appears more than once and returns
multiple values, for example checkbox.

GET Method Example Using URL:


Below is [Link] servlet program to handle input given by web browser. We are
going to use getParameter() method which makes it very easy to access passed information:

// Import required java libraries


import [Link].*;
import [Link].*;
import [Link].*;

public class Formdatausinggetmethod extends HttpServlet {

/* Processes requests for both HTTP <code>GET</code> and <code>POST</code>


methods.
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
PrintWriter out = [Link]();
try {
// TODO output your page here

[Link]( "<html>");
[Link]( "<head><title> Using GET Method to Read Form Data </title></head>");
[Link]( "<body bgcolor=pink");

[Link]("<form action=Formdatausinggetmethod method=GET>");

[Link]("First Name: <input type=text name=first_name>");


[Link]("<br />");
[Link]("Last Name: <input type=text name=last_name />");
[Link]("<input type=submit value=Submit />");
[Link]("</form>");

[Link]( "<h1 align=center> Using GET Method to Read Form Data </h1>");
[Link]( "<ul>");
[Link]( " <li><b>First Name</b>: ");
[Link]( [Link]("first_name"));
[Link]( " <li><b>Last Name</b>: ");
[Link]( [Link]("last_name"));
[Link]( "</ul>");
[Link]( "</body></html>");

8
} finally {
[Link]();
}
}

After running, try to enter First Name and Last Name and then click submit button to see the result on
your local machine where tomcat is running. Based on the input provided, it will generate similar result
as shown below.

If you click submit button you will get the query data at the URL address as follows

[Link]

If everything goes fine, above compilation would produce [Link] file. Next you
would have to copy this class file in <Tomcat-installation-directory>/webapps/ROOT/WEB-INF/classes
and create following entries in [Link] file located in <Tomcat-installation-
directory>/webapps/ROOT/WEB-INF/

9
<servlet>
<servlet-name> Formdatausinggetmethod </servlet-name>
<servlet-class> Formdatausinggetmethod </servlet-class>
</servlet>

<servlet-mapping>
<servlet-name> Formdatausinggetmethod </servlet-name>
<url-pattern>/ Formdatausinggetmethod </url-pattern>
</servlet-mapping>

POST Method Example Using Form:


Let us do little modification in the above servlet, so that it can handle GET as well as POST methods.
Below is [Link] servlet program to handle input given by web browser using
GET or POST methods.

// Import required java libraries


import [Link].*;
import [Link].*;
import [Link].*;

public class Formdatausingpostmethod extends HttpServlet {

/*Processes requests for both HTTP <code>GET</code> and <code>POST</code>


methods. */

protected void processRequest(HttpServletRequest request, HttpServletResponse


response)
throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
PrintWriter out = [Link]();
try {
// TODO output your page here

10
[Link]( "<html>");
[Link]("<head><title> Using POST Method to Read Form Data </title></head>");
[Link]( "<body bgcolor=pink");

[Link]("<form action=Formdatausingpostmethod method=POST>");


[Link]("First Name: <input type=text name=first_name>");
[Link]("<br />");
[Link]("Last Name: <input type=text name=last_name />");
[Link]("<input type=submit value=Submit />");
[Link]("</form>");

[Link]("<h1 align=center> Using POST Method to Read Form Data </h1>");


[Link]( "<ul>");
[Link]( " <li><b>First Name</b>: ");
[Link]( [Link]("first_name"));
[Link]( " <li><b>Last Name</b>: ");
[Link]( [Link]("last_name"));
[Link]( "</ul>");
[Link]( "</body></html>");
} finally {
[Link]();
}
}

Based on the input provided, it would generate similar result as mentioned in the above examples.

11
Passing Checkbox Data to Servlet Program
Checkboxes are used when more than one option is required to be selected.

Here is example HTML code, [Link], for a form with two checkboxes

<html>
<body>
<form action="CheckBox" method="POST" target="_blank">
<input type="checkbox" name="maths" checked="checked" /> Maths
<input type="checkbox" name="physics” /> Physics
<input type="checkbox" name="chemistry" checked="checked" />
Chemistry
<input type="submit" value="Select Subject" />
</form>
</body>
</html>

The result of this code is the following form

Maths Physics Chemistry

Below is [Link] servlet program to handle input given by web browser for checkbox button.

// Import required java libraries


import [Link].*;
import [Link].*;
import [Link].*;

public class Formdatacheckbox extends HttpServlet {

12
/* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
methods
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
PrintWriter out = [Link]();
try {
// TODO output your page here
[Link]("<html>");
[Link] ("<head><title> Reading checkbox data</title></head>");
[Link] ("<body bgcolor=#f0f0f0>");
[Link]("<form action=Formdatacheckbox method=POST>");
[Link]("<input type=checkbox name=maths checked=checked /> Maths");
[Link]("<input type=checkbox name=physics /> Physics");
[Link]("<input type=checkbox name=chemistry checked=checked />Chemistry");

[Link]("<input type=submit value=Select Subject />");


[Link]("</form>");
[Link]("<h1 align=center> Reading data checkbox data</h1>");
[Link]("<ul>");
[Link]("<li><b>Maths Flag : </b> ");
[Link]( [Link]("maths"));
[Link](" <li><b>Physics Flag : </b> ");
[Link]( [Link]("physics"));
[Link]("<li><b>Chemistry Flag : </b> ");
[Link]( [Link]("chemistry"));
[Link]("</ul>");

[Link](" </body>");
[Link](" </html>");

} finally {
[Link]();
}
}

Reading All Form Parameters:


Following is the generic example which uses getParameterValues() method of HttpServletRequest to
read all the available form parameters. This method returns an Enumeration that contains the parameter
names in an unspecified order.

import [Link].*;
import [Link].*;
import [Link].*;

13
public class checkbox extends HttpServlet {

protected void processRequest(HttpServletRequest req, HttpServletResponse res) throws


ServletException, IOException {

[Link]("text/html");
PrintWriter out = [Link]();
try {

// TODO output your page here


[Link]("<html>");
[Link]("<head>");
[Link]("<title>Check Box Example</title>");
[Link]("</head>");
[Link]("<body bgcolor=pink>");
[Link]("<form method=post action=checkbox>");
[Link]("<h3>Select Courses</h3>");
[Link]("<p><input type=checkbox name=course value=[Link] /> [Link]");
[Link]("<p><input type=checkbox name=course value=C++ /> C++ ");
[Link]("<p><input type=checkbox name=course value=PHP /> PHP");
[Link]("<p><input type=submit value=Submit /> </p>");
[Link]("</form> ");
String[] courses;
courses= [Link]("course");
if(courses!=null)
{
[Link]("Selected course(s) is/are:");
[Link]("<ul>");
for(int i=0; i<[Link]; i++){
[Link]("<li>"+(courses[i]));
}
[Link]("</ul>");

}
else
[Link]("<p><font color=red>not selected</font></p>");

[Link]("</body></html>");
} finally { [Link](); }
}
}

Retrieving Data from the table using Statement

Let we have a database called stud and table dept1 (with fields did and dname)

In this program we are going to fetch the data from the database table to our java servlet pages.

14
To accomplish our goal we first have to make a class named as dbconnection which must extends the
abstract HttpServlet class, the name of the class should be such that the other person can understand what
this program is going to perform. The logic of the program will be written inside the processRequest()
method which takes two arguments, first is HttpServletRequest interface and the second one is the
HttpServletResponse interface and this method can throw ServletException.

Inside this method call the getWriter() method of the PrintWriter class. We can retrieve the data from the
database only and only if there is a connectivity between our database and the java program. Now use the
static method getConnection() of the DriverManager class. This method takes three arguments and returns
the Connection object. SQL statements are executed and results are returned within the context of a
connection. Now your connection has been established. Now use the method createStatement() of the
Connection object which will return the Statement object. This object is used for executing a static SQL
statement and obtaining the results produced by it. As we need to retrieve the data from the table so we
need to write a query to select all the records from the table. This query will be passed in the
executeQuery() method of Statement object, which returns the ResultSet object. Now the data will be
retrieved by using the getString() method of the ResultSet object.

import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class dbconnection extends HttpServlet {
Connection con;Statement stmt; ResultSet rs;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();

try {

[Link](new [Link]());
con=[Link]("jdbc:oracle:thin:@localhost:1521:xe", "stud", "stud");
stmt=[Link]();

[Link]("Deriver loaded ");

rs=[Link]("select * from dept1");

[Link]("<html>");
[Link]("<head>");
[Link]("<title>Servlet dbconnection</title>");
[Link]("</head>");

15
[Link]("<body>");
[Link]("<table border= 2> ");
[Link]("<tr> ");
[Link]("<td> did </td> <td> dname </td> ");
[Link]("</tr> ");
[Link]("<br>");

while([Link]()){
[Link]("<tr> ");
[Link]("<td>"+[Link](1) + "</td> <td>" +[Link](2)+"</td>" );
[Link]("<br>");
}

}
catch(Exception e){
[Link](e);
}

Inserting data from the HTML page to the database

Let we have a database called stud and table dept1 (with fields did and dname)

In this program we are going to make servlet program in which we are going to insert the values in the
database table from the html form.

To make our program working we need to make one html form in which we will have two fields, one is
for the department name and the other one is for entering the department id. And we will have the submit
button, clicking on which the values will be passed to the the database dserver.

The values which we have entered in the Html form will be retrieved by the server side program which
we are going to write. To accomplish our goal we first have to make a class named as
Servletinsertingdatatodb which must extends the abstract HttpServlet class, the name of the class should
be such that the other person can understand what this program is going to perform. The logic of the
program will be written inside the processRequest() method which takes two arguments, first is

16
HttpServletRequest interface and the second one is the HttpServletResponse interface and this method
can throw ServletException.

We can insert the data in the database only and only if there is a connectivity between our database and
the java program. To establish the connection between our database and the java program the static method
getConnection() of the DriverManager class takes three arguments and returns the Connection object.
SQL statements are executed and results are returned within the context of a connection. Now your
connection has been established. Now use the method prepareStatement() of the Connection object
which will return the PreparedStatement object to submit query to database. The values which we have
got from the html will be set in the database by using the setString() method of the PreparedStatement
object.

import [Link].*;

import [Link].*;

import [Link].*;

import [Link].*;

public class Servletinsertingdatatodb extends HttpServlet {

Connection con; Statement stmt; ResultSet rs;

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

[Link]("text/html");

PrintWriter out = [Link]();

[Link](" <html>");

[Link](" <head>");

[Link]("<title>Wel come to data insertion page</title> </head>");

[Link]("<body>");

[Link]("<form method=POST action=Servletinsertingdatatodb >");

[Link](" <p>Enter Dept ID: <input type=text name=did size=20></p>");

[Link](" <p>Enter Dept Name: <input type=text name=dname size=20 </p>");

17
out. [Link]("<input type=submit value=save ></p>");

println("<input type=reset value=clear ></p>");

[Link]("</form>");

try { [Link](new [Link]());

con=[Link]("jdbc:oracle:thin:@localhost:1521:xe", "stud", "stud");

stmt=[Link]();

[Link]("<h1> Deriver loaded </h1>");

String id = [Link]("did");

String name = [Link]("dname");

PreparedStatement pst = [Link]("insert into dept1 values(?,?)");

[Link](1,id);

[Link](2,name);

int i = [Link]();

if(i!=0){

[Link]("<br>Record has been inserted");

} else

{ [Link]("failed to insert the data");

[Link]("</body>");

[Link]("</html>");

} catch (Exception e){

[Link](e); }

18
Example: Create the following servlet pages

//Home .java

try { [Link]("<html>");
[Link]("<head>");
[Link]("<title>Well Come To DMU Student Registration</title>");
[Link]("</head>");
[Link]("<body bgcolor=#66CCFF>");
[Link]("<center><table>");
[Link]("<tr><th colspan=5><h1 style=color:#cc6600>Debre Markos University<br>Online student
Registration System</h1></th></tr>");
[Link]("<tr><td><a href=Home style=color:white;font-size:26><input type=submit value=Home
style=color:white;font-size:20;background-color:#cc6600></a></td>");

[Link]("<td><a href=ContactUs style=color:white;font-size:26><input type=submit value='Contact Us'


style=color:white;font-size:20;background-color:#cc6600></a> </td>");

[Link]("<td><a href=AboutUs style=color:white;font-size:26><input type=submit value='About Us'


style=color:white;font-size:20;background-color:#cc6600></a> </td>");

[Link]("</tr>");

[Link]("</table></center>");

[Link]("</br></br></br></br></br></br></br>");

[Link]("<div style=height:100;width:200;margin-top:8;margin-right:1000");

[Link]("<tr><td><form method=post action=Validateuser>"

+ "<h1>Login</h1>"

+ " Enter User Name:<input type=text name=username></br>"

+ "Enter Password:<input type=password name=password></br>"

+ "<input type=submit value=Login><input type=Reset value=Reset> </td></tr></form>");

[Link]("</div>");

[Link]("</body>");

[Link]("</html>");

} finally { [Link](); }

19
//[Link]

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

[Link]("text/html");

PrintWriter pw = [Link]();

String name = [Link]("username");

String password = [Link]("password");

RequestDispatcher rd; //used for page redirection

if([Link]("") && [Link]("")){

[Link](" User name and password can't be empty");

rd=[Link]("/Home"); //return to Home page

[Link](request, response);

else if ([Link]("stud") && [Link]("123")) {

20
rd=[Link]("/DataAccesshomepage"); //redirect to DataAccesshomepage

[Link](request, response);

} else{

[Link](" Try to enter correct user name or password");

rd=[Link]("/Home");

[Link](request, response);

} }

//DataAccesspagehome

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

[Link]("text/html;charset=UTF-8");

PrintWriter out = [Link]();

try { [Link]("<html>");
[Link]("<head>");
[Link]("<title>Well Come To DMU Student Registration</title>");
[Link]("</head>");
[Link]("<body bgcolor=#66CCFF>");
[Link]("<center><table>");
[Link]("<tr><th colspan=5><h1 style=color:#cc6600>Debre Markos University<br>Online student
Registration System</h1></th></tr>");
[Link]("<tr><td><a href=Home style=color:white;font-size:26><input type=submit value=Home
style=color:white;font-size:20;background-color:#cc6600></a></td>");

[Link]("<td><a href=StudentManagement style=color:white;font-size:26><input type=submit


value='Student Management' style=color:white;font-size:20;background-color:#cc6600></a> </td>");

[Link]("<td><a href=CourseManagement style=color:white;font-size:26><input type=submit


value='Course Management' style=color:white;font-size:20;background-color:#cc6600></a> </td>");

[Link]("<td><a href=ResultManagement style=color:white;font-size:26><input type=submit


value='Result Management' style=color:white;font-size:20;background-color:#cc6600></a> </td>");

[Link]("</tr>");

[Link]("</table></center>");

[Link]("</br></br></br></br></br></br></br>");

21
[Link]("</div>");

[Link]("<div style=height:100;width:200;margin-top:-200;margin-left:1100");

[Link]("<a href=[Link]><input type=submit value=Logout></a>");

[Link]("</div>");

[Link]("</body>");

[Link]("</html>");

} finally {

[Link]();

} }

//[Link]

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

[Link]("text/html;charset=UTF-8");

PrintWriter out = [Link]();

try {

[Link]("<html>");

22
[Link]("<head>");

[Link]("<title>Well Come To DMU Student Registration</title>");

[Link]("</head>");

[Link]("<body bgcolor=#66CCFF>");

[Link]("<center><table>");

[Link]("<tr><th colspan=5><h1 style=color:#cc6600>Debre Markos University<br>Online student


Registration System</h1></th></tr>");

[Link]("<tr><td><a href=Home style=color:white;font-size:26><input type=submit value=Home


style=color:white;font-size:20;background-color:#cc6600></a></td>");

[Link]("<td><a href=DataAccesshomepage style=color:white;font-size:26><input type=submit


value='Data access home' style=color:white;font-size:20;background-color:#cc6600></a> </td>");

[Link]("<td><a href=Addnewstudent style=color:white;font-size:26><input type=submit value='Add


new student' style=color:white;font-size:20;background-color:#cc6600></a> </td>");

[Link]("<td><a href=Updatestudentprofile style=color:white;font-size:26><input type=submit


value='Update student Profile' style=color:white;font-size:20;background-color:#cc6600></a> </td>");

[Link]("<td><a href=DeleteStudent style=color:white;font-size:26><input type=submit value='Delete


student Profile' style=color:white;font-size:20;background-color:#cc6600></a> </td>");

[Link]("<td><a href=Searchstudent style=color:white;font-size:26><input type=submit value='Search


student' style=color:white;font-size:20;background-color:#cc6600></a> </td>");

[Link]("</tr>");

[Link]("</table></center>");

[Link]("</br></br></br></br></br></br></br>");

[Link]("</div>");

[Link]("<div style=height:100;width:200;margin-top:-200;margin-left:1100");

[Link]("&nbsp;&nbsp;<a href=Logout ><input type=submit value='Logout' ></a>");

[Link]("</div>");

[Link]("</body>");

[Link]("</html>");

} finally {

23
[Link]();

//[Link]

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

[Link]("text/html;charset=UTF-8");

PrintWriter out = [Link]();


try {
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Well Come To DMU Student Registration</title>");
[Link]("</head>");
[Link]("<body bgcolor=#66CCFF>");
[Link]("<center><table>");
[Link]("<tr><th colspan=5><h1 style=color:#cc6600>Debre Markos University<br>Online student
Registration System</h1></th></tr>");
[Link]("<tr><td><a href=Home style=color:white;font-size:26><input type=submit value=Home
style=color:white;font-size:20;background-color:#cc6600></a></td>");

[Link]("<td><a href=DataAccesshomepage style=color:white;font-size:26><input type=submit


value='Data access home' style=color:white;font-size:20;background-color:#cc6600></a> </td>");

[Link]("<td><a href=Addnewstudent style=color:white;font-size:26><input type=submit value='Add


new student' style=color:white;font-size:20;background-color:#cc6600></a> </td>");

[Link]("<td><a href=Updatestudentprofile style=color:white;font-size:26><input type=submit


value='Update student Profile' style=color:white;font-size:20;background-color:#cc6600></a> </td>");

[Link]("<td><a href=DeleteStudent style=color:white;font-size:26><input type=submit value='Delete


student Profile' style=color:white;font-size:20;background-color:#cc6600></a> </td>");

[Link]("<td><a href=Searchstudent style=color:white;font-size:26><input type=submit value='Search


student' style=color:white;font-size:20;background-color:#cc6600></a> </td>");

[Link]("</tr>");
[Link]("</table></center>");
[Link]("</br></br></br></br></br></br></br>");
[Link]("</div>");
[Link]("<div style=height:100;width:200;margin-top:-200;margin-left:1100");
[Link]("&nbsp;&nbsp;<a href=Logout ><input type=submit value='Logout' ></a>");

24
[Link]("</div>");
[Link]("</body>");
[Link]("</html>");
} finally {
[Link]();
} }

//[Link] ,let we have studentdb database and student table(sis,sname,dname)

import [Link].*;

import [Link].*;

import [Link].*;

import [Link].*;

public class AddNewStudent extends HttpServlet {

Connection con; Statement stmt; ResultSet rs;

protected void processRequest(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
PrintWriter out = [Link]();
try {

25
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Well Come To DMU Student Registration</title>");
[Link]("</head>");
[Link]("<body bgcolor=#66CCFF>");
[Link]("<center><table>");
[Link]("<tr><th colspan=5><h1 style=color:#cc6600>Debre Markos University<br>Online student
Registration System</h1></th></tr>");
[Link]("<tr><td><a href=Home style=color:white;font-size:26><input type=submit value=Home
style=color:white;font-size:20;background-color:#cc6600></a></td>");
[Link]("<td><a href=DataAccesshomepage style=color:white;font-size:26><input type=submit
value='Data access home' style=color:white;font-size:20;background-color:#cc6600></a> </td>");
[Link]("<td><a href=Addnewstudent style=color:white;font-size:26><input type=submit value='Add
new student' style=color:white;font-size:20;background-color:#cc6600></a> </td>");
[Link]("<td><a href=Updatestudentprofile style=color:white;font-size:26><input type=submit
value='Update student Profile' style=color:white;font-size:20;background-color:#cc6600></a> </td>");
[Link]("<td><a href=DeleteStudent style=color:white;font-size:26><input type=submit value='Delete
student Profile' style=color:white;font-size:20;background-color:#cc6600></a> </td>");
[Link]("<td><a href=Searchstudent style=color:white;font-size:26><input type=submit value='Search
student' style=color:white;font-size:20;background-color:#cc6600></a> </td>");

[Link]("</tr>");
[Link]("</table></center>");
[Link]("</br></br></br></br></br></br></br>");
[Link]("</div>");
[Link]("<div style=height:100;width:200;margin-top:-200;margin-left:1100");
[Link]("&nbsp;&nbsp;<a href=Home ><input type=submit value='Logout' ></a>");
[Link]("</div>");
[Link]("<center>");
[Link]("<table>"); [Link]("<tr><th colspan=2>Student Registration Form</th></tr>");
[Link]("<tr><td><form method=post action=AddNewStudent>");

[Link]("<tr><td>Student ID:</td><td><input type=text name=id></td></tr>");

[Link]("<tr><td>Student Name:</td><td><input type=text name=name></td></tr>");

[Link]("<tr><td>Student Department:</td><td><input type=text name=dept></td></tr>");

[Link]("<tr><td></td><td><input type=submit value=Save><input type=Reset value=Reset></td></tr>");

[Link]("</form>");

[Link]("</center>");

{ [Link](new [Link]());

26
con=[Link]("jdbc:oracle:thin:@localhost:1521:xe", "stududentdb",
"stududentdb");

stmt=[Link]();

[Link]("<h1> Deriver loaded </h1>");

String id = [Link]("sid");

String name = [Link]("sname");

String dept = [Link]("dname");

PreparedStatement ps= [Link]("insert into student values(?,?,?)");

[Link](1,id);

[Link](2,name);

[Link](3,dept);

int i = [Link]();

if(i!=0){

[Link]("<br>Record has been inserted");

} else

{ [Link]("failed to insert the data");

[Link]("</body>");

[Link]("</html>");

} finally {

[Link]();

} }

27
28

You might also like