Update Records Into M.S Access/SQL Table Using Java Servlet Programming


Home  »  Java  »  Advance Java  »  Java Servlet  »  Update Reocords


The following are two Java Servlet Server side programs and one HTML Client Side. HTML Program will send client side information for find out a specific or selected data from M.S.Access/SQL table and Java Servlet (Server Side) program will display specific records from database. and second java servlet program will update records into M.S.Access Table. For this first of all you need to create a M.S.Access database or SQL... but in this example I'm using M.S.Access. Create one table in M.S Access as shown below:






Table Definitions:


Table Name: [ICTCashBook.mdb]-Download
Primary Key: VoucherNo
Foreign Key: Nil

Column Definition:
Column NameData Type
IDAutoNumber
SessionText
MonthText
DDNumber
MMNumber
YYNumber
VoucherNoNumber
ArticlesMemo
FromWhichPurchsedMemo
AmountNumber
RemarksMemo

Table Description:
Column NameDescription
IDCreate AutoNumber for image id
Sessioninsert Session for ex. 2011-12
Monthinsert month in text...
DDinsert date in numeric
MMinsert month in numeric
YYinsert year in numeric
VoucherNoinsert unique numeric no
Articlesinsert text
FromWhichPurchsedinsert text
Amountinsert numeric value
Remarksinsert text/numeric value



After create table in M.S. Access database now you will create DSN.







Following is the Java Servlet source code of the UpdateIncomeExpenditure.java





Now, second java servlet program...





"Start -> All Programs -> Apache Tomcat 4.0 -> Start Tomcat." and minimize window


How to Install Apache Tomcat 4.0 then Click Here

and for Download Apache Tomcat 4.0 then Click Here


Now compile the above both code using javac compiler...









Now, copy "UpdateIncomeExpenditure.class" and "UpdateIncomeExpenditures.class" Files from c:/jdk1.4/bin/ and then paste it under below dir...



"C:/Program Files/Apache Tomcat 4.0/webapps/examples/WEB-INF/classes/"



or use below command











or you can use "*.class" for copy all classes...







Now Open any web browser and type client side URL into address bar as below...




HTML Form for retrieve selecting information






Open Records for Updating



Final Update Reocrd using Java Servlet Programming



User Authentication Form Using Servlet

Using JDBC In Java Servlet Programming

Calling a servlet from within an HTML page

Generic and Http Servlets

Creating Java Servlets (Compiling and Running a Java Servlet)

Home  »  Java  »  Advance Java  »  Java Servlets  »  Creating Java Servlets


A Servlet is like any other Java program - Basically a class.

To create a Servlet class, that class must be subclassed either form the GenericServlet class or some other subclass of the GenericServlet class.

Servlets are more often that not used to provide dynamic content on the World Wide Web. The Servlet API consists of certain specialized classes and interfaces, which provide users with the necessary properties and methods to implement servlets on the Web.


The HttpServlet Abstract Class

The HttpServlet class is an abstract class that simplifies writing HTTP serlvets. It extends the GenericServlet base class and provides a framework for handling the HTTP protocol. Because it is an abstract class, servlet writers must subclass it and override at least one method.

The methods normally overriden are:

  •    doGet()

  •    doPost()



The lifecycle methods init() and destroy() can also be overriden if the servlet is managing resources that are held for the lifetime of the servlet. Servlets that do not manage resources do not need to spcialize these methods.

The service() method is not typically overriden. The service() method, as provided supports standard HTTP requests by dispatching them to appropriate methods, such as the methods listed above that have the prefix "do".

The service() method

Declaration



This is an HTTP specific version of the service() method, which accepts HTTP specific parameters, This method is rarely overriden. Standard HTTP requests such as GET and POST are supported by dispatching the requests to specialized methods such as doGet(0 and doPost().



Parameters

HttpServletRequest - This parameter encapsulates the HTTP request to the servlet.
HttpServletResponse - This parameter encapsulates the servlet's response to an HTTP.




The HttpServletRequest Interface

The HttpServletRequest interface encapsulates information of the request made to a servlet. This interface gets information from the client to the servlet for use in the service() method. It allows HTTP protocol specified header information to be accessed from the service() method.


HttpServletRequest Methods

getMethod()Returns the HTTP method (GET, POST) with which the request was made.
getQueryString()Returns any Query String that is part of the HTTP request
getRemoteUser()Returns the name of the user making the HTTP request
getRequestedSessionId()Returns the Session Id specified with the HTTP request
getSession(boolean)If boolean is FALSE, it returns the current valid session associated witht the HTTP request. If boolean is TRUE it creates a new session
IsRequestSessionIdValid()Checks whether the HTTP request is associated with a session that is valid in the current session context.
getCookies()Returns the array of cookies found in the HTTP request.
Table 24.1 HttpServletRequest Methods




The HttpServletResponse Interface

The HttpServletResponse interface encapsulates information of the response sent by a servlet to a client. This interface allows a servlet's service() method to manipulate HTTP protocol specified header information and return data to its client.


HttpServletResponse Methods

addCookie(Cookie)Adds the specified cookie to the response
encodeUrl(String)Encodes the specified URL by adding the session id to it. If encoding it not required, it returns the URL unchanged.
sendRedirect(String)Sends a temporary redirect response to a client using the specified redirect location URL.
sendError(int)Sends an error response to a client using the specified status code and a default message.
Table 24.2 HttpServletResponse Methods




Creating a Java Servlet

To write a Java servlet, you need to import the javax.servlet.* package of Java. The Java servlet are executed on the Web Server. The following program code creates a servlet that prints a text message on the Web browser using the println() method of Java servlet.




Compiling and Running a Java Servlet

Servlet are compiled using the javax.servlet and javax.servlet.http package of Java. If the compiler does not have these packages, then yo neeed install Java Servlet Development Kit (JSDK) and include the JSDK classes in the CLASSPATH to compile the servlet.

To compile and run the java servlet first of all you need to install any java development kit and then web server like Apache Tomcat 4.0 after installing both now copy javax folder and then paste it into bin dir - "c:/jdk1.3/bin/"


Installing Apache Tomcat 4.0


Diagram 24.3

Diagram 24.4

Diagram 24.5

Diagram 24.6

Diagram 24.7

Diagram 24.8

Diagram 24.9

Diagram 24.10


Download - Java

Download - Apache Tomcat 4.0

more » Apache Tomcat Versions

Download - javax

After install Apache Tomcat 4.0 now Start it from Diagram 24.10 or...

"Start -> All Programs -> Apache Tomcat 4.0 -> Start Tomcat." and minimize window

Now compile Java Servlet Program using below command as shown diagram 24.11


Diagram 24.11 Compile Java Servlet

Now, copy "SimpleServlet.class" File and then paste it under

"C:/Program Files/Apache Tomcat 4.0/webapps/examples/WEB-INF/classes/"


Diagram 24.11 Copy SimpleServlet.class file under classes dir

Now, Specifying the URL of the Java Servlet in the address bar of the Web browser. The Syntax to invoke a servlet file from the browser in Apache Tomcat Java Web Server 4.0.3 is:



The above code creates a sample Java Servlet that displays a text message on the Web browser.

Diagram 24.12 shows the Web browser window a text message displayed on it.


Diagram 24.12 Output of Running a Java Servlet


Java Servlet Programming







Java Servlets

Home  »    Java   »    Advance Java  »  Java Servlets


Java servlets are the Java programs that are used to handle server-side operations in client-server architecture. CGI scripts were used prior to Java servlet for client-server computing. CGI scripts were written in C or Perl programming language. Java servlets are platform independent and therefore provide enhanced features for client-server operations. Java compiled Java servlets can be invoked multiple times by the Web servers without any need of recompilation. You need not compile sevlet each time they are executed because the init() method in Java servlet is invoked only once in a servlet life cycle. Once the servlet is initialised and compiled, it converts into the native code. This native code of Java servlet is now ready to handle client requests without any need of recompilation. This feature of Java servlet makes it more suitable to handle server-side requests on heterogeneous networks such as the Internet. The various steps involved in using Java servlet to handle server-side requests are:

  1. The client program running on the Web browser sends and Http request to the Web server.
  2. The Web server accepts the client's request, loads and executes the Java servlet in the Java Virtual Machine (JVM) and forwards the client's request to the servlet.
  3. The servlet performs the operation specified by the Http request and sends the result of the desired operation back to the Web server.
  4. The Web server sends to response back to the requesting client using the HttpResponse() method of the Java servlet.



Using a Java Servlet


Diagram Using a Java Servlet


A Java servlet is a Java class that implements the javax.servlet.Servlet interface of Java. There are two types of Java servlet, generic servlet that implements the javax.servlet.GenericServlet interace and the Http servlet, which implements the javax.servlet.http.HttpServlet interface.



Features of Servlets

JAVA Servlets Provides various Services which helps in accessing the Resources those are Located on the Server and There are Many Advantages over using the Servlets.

  1. Persistent : The Persistent Features of Servlet Makes it , as a Server which handles Request of Many Clients at a same Time without Recompiling a Servlet.

  2. Fast : Servlets Provides fast Services to the Client because they are easily Loaded and they Loaded only once in the Memory.

  3. Platform Independent :Servlets are Executed on any Machine once Compiled.

  4. Secure : Servlets are very Secure because they doesn't Provide an information that how the Request of a Client is Processed.

  5. Efficient : As I Mention Earlier that Servlet of java can handle Request of Many users. So this Makes it Efficient.

  6. Portables : Servlets are Portable Means they can run on various Platforms without Modifications.



Requirements of JAVA Servlets

If we wants to Execute Servlets o our Machine then first we have to fulfills all the Software's Requirements which a Servlet uses. Requirements of JAVA Servelets are as below:-

  1. JDK : This is the Main Component for Executing JAVA Servelets So first of all install JAVA on the Machine by using the JAVA development Kit Which provides us various Methods and interfaces for Running Programs of JAVA.

  2. Servlet API : For Executing you have to compile your Servlet by using the Servlet.Jar File by using the Classpath and we can also set the Classpath in which Servelet.jar File resides.

  3. Servlet-Enabled Web Server : Then after Compiling we have to start the Server which will Listen the Request of Client and there are Many Web Servers Available and with the help of those Web Servers handle the Request of Clients. The Various Types of Web Servers are as followings :-

    1. Apache Web Server : This is the stand alone Server which can be used for testing the JAVA servlet and this is very Reliable Server.
    2. Tomcat Server.
    3. Jigsaw Server.
    4. Atlanta Web Server : This is the very useful Software which can be installed on any System like Solaris , MacOS and also on Unix Operating system.
    5. AVA web Server: This Server has a capability to run the JAVA Servlets those are Written only in the JAVA Programming Language.


Servlets API

A Servlet is much like an applet it does not run any application or doesn't have a main() method rather it is loaded in the memory and instance is created and it also provides some methods like an applet but these are different from applet's methods. When a servlet instance is created its init() method is called. But a servlet does not need to become active or de-active like applet. So it does not have start(), stop() or paint() methods. But these are required to give the response to new connections. When a new connection is detected it automatically call the service() method. The service() method takes two arguments first ServletRequest and second is ServletResponse. These interface provides the access methods that allow how a servlet will be executed, what request it has received and what to do to provide a proper response. Basically we can use HTTP for communication. Se we can use HTTP servlet class, which is the subclass of Generic servlet. The HTTP defines two interfaces –

  1. HTTP ServletRequest interface
  2. HTTP ServeletResponse interface
The first interface is used for giving the details of the request, path and query string, and the second is used for sending the HTTP errors and for redirects.


Servlet Life Cycle

A Java servlet extends the HttpServlet class of Java. A Java servlet invokes the init(), service() and destroy() methods during its life cycle. The various steps in a servlet life cycle are:

  1. Create a servlet instance.

  2. The Web server invokes the init(ServletConfig config) method of Java servlet. The argument, config in the init(ServletConfig config) method is an instance of the ServletConfig class. It stores servlet parameters and a refence to the servlet.

  3. The init() method initialises the servlet and is invoked only once in the servlet life cycle.

  4. The service() method is invoked to handle the request made by the Web browser to the Java servlet. You can override the service() method of the Java servlet.

  5. The destroy() method is invoked to destroy a Java servlet and free all the resources that a Java servlet occupies. The destroy() method is also invoked only once in a servlet lifecyle.


Diagram shows a servlet lifecycle.


Servlet Lifecycle


There are three levels in a servlet lifecycle. The load and unload events of a Java servlet call the init() and destroy() methods, respectively. The service() method of the Java servlet is invoked to handle the client requests.



Java Servlet Programming







<< Previous Topic
Next Topic >>