Update Records Into M.S Access/SQL Table Using Java Servlet Programming
| Home | » | Java | » | Advance Java | » | Java Servlet | » | Update Reocords |
|---|
Column Definition:
Table Description:
|
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 |
|---|
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:
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
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. |
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. |
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
|
|
|
|
|
|
|
|
|
|
|
|
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 |
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
- Creating Java Servlets (Compiling and Running a Java Servlet)
- Generic and Http Servlets
- Calling a servlet from within an HTML page
- Using JDBC In Servlet
- User Authentication Form using Java Servlet
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:
- The client program running on the Web browser sends and Http request to the Web server.
- 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.
- The servlet performs the operation specified by the Http request and sends the result of the desired operation back to the Web server.
- The Web server sends to response back to the requesting client using the HttpResponse() method of the 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.
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.
Fast : Servlets Provides fast Services to the Client because they are easily Loaded and they Loaded only once in the Memory.
Platform Independent :Servlets are Executed on any Machine once Compiled.
Secure : Servlets are very Secure because they doesn't Provide an information that how the Request of a Client is Processed.
Efficient : As I Mention Earlier that Servlet of java can handle Request of Many users. So this Makes it Efficient.
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:-
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.
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.
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 :-
- Apache Web Server : This is the stand alone Server which can be used for testing the JAVA servlet and this is very Reliable Server.
- Tomcat Server.
- Jigsaw Server.
- 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.
- 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 –
- HTTP ServletRequest interface
- HTTP ServeletResponse interface
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:
-
Create a servlet instance.
-
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.
-
The init() method initialises the servlet and is invoked only once in the servlet life cycle.
-
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.
-
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.
|
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
- Creating Servlet
- Generic and Http Servlets
- Calling a servlet from within an HTML page
- Using JDBC In Servlet
- User Authentication Form using Java Servlet














