answersLogoWhite

0


Best Answer

// You can use this sample servlet as a starting point to connect to

// your database included with useractive account.

//

// Portions copied from Sun's HelloWorldServlet.java

// You are free to use this code in any way you chose

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.sql.*;

public

class HelloWorldServlet extends HttpServlet {

public void doGet (HttpServletRequest req, HttpServletResponse res)

throws ServletException, IOException {

res.setContentType("text/html");

ServletOutputStream out = res.getOutputStream();

out.println("<html>");

out.println("<head><title>Hello World</title></head>");

out.println("<body>");

try {

// The newInstance() call is a work around for some

// broken Java implementations

out.println("asdf");

Class.forName("org.gjt.mm.mysql.Driver").newInstance();

}

catch (Exception E) {

out.println("Unable to load driver.");

E.printStackTrace();

}

out.println("<br><hr>");

try {

Connection Conn =

DriverManager.getConnection("jdbc:mysql://sql.useractive.com/USERNAME?user=USERNAME&password=PASSWORD");

// Do something with the Connection

Statement Stmt = Conn.createStatement();

ResultSet RS = Stmt.executeQuery("SELECT * from SOMETABLE");

while (RS.next()) {

out.println(RS.getString(1));

}

// Clean up after ourselves

RS.close();

Stmt.close();

Conn.close();

}

catch (SQLException E) {

out.println("SQLException: " + E.getMessage());

out.println("SQLState: " + E.getSQLState());

out.println("VendorError: " + E.getErrorCode());

}

out.println("<h1>Hello World. Sun... 1.4</h1>");

out.println("</body></html>");

}

public String getServletInfo() {

return "Create a page that says <i>Hello World</i> and send it back";

}

}

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a program using servlet and JDBC for developing an onine application to create a database?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Computer Science

Importance of deployment descriptor in servlet?

The deployment descriptor is an xml file that contains the basic and most important information that is required to deploy a web application (Servlet) Without this, the web server would not know, which requests to entertain/consider as requests to access this servlet.


What is the difference between a server and a web server?

== == The basic difference between a web server and an application server is Webserver can execute only web applications i,e servlets and JSPs and has only a single container known as Web container which is used to interpret/execute web applications Application server can execute Enterprise application, i,e (servlets, jsps, and EJBs) it is having two containers 1. Web Container(for interpreting/executing servlets and jsps) 2. EJB container(for executing EJBs). it can perform operations like load balancing , transaction demarcation etc etc


How do you get ServletContext object in EJB - 38k?

get servlet context path from EJB


What is JAVA Servlet Development Kit?

Java Servlet Development Kit is an integrated development kit used to build, test, and deploy Java Servlet applications. JSDK allows most standard Web server such as Netscape servers, IIS, Apache and others to load servlets . JSDK is a suite of software for easing the development of Java servlets.


Where you can download java servlet development kit2.0?

There is no specific/separate servlet development kit. Servlets can be developed and compiled using the standard JDK and JRE. The only difference being the fact that - to run them you need a web server

Related questions

What is the use of Servlet in java?

Java Servlet is used for Server Side programming for developing Web Applications. It easily employs Database Connectivity. We can also use JSP however it cannot replace a Java Servlet.


Differentiate Java applet and Java servlet?

Java Applet is an application designed to transmit on internet to execute on java compatible browsers. Java Servlet is a server side program used to provide services to clients.


Which one is faster servlet OR jsp?

Servlet is more faster than JSP, but JSP is more convenient than Servlet and JSP is clearly superior, shorter, simple and easier to use. JSP can be perceived as Java in HTML code. JSP require no explicit compilation as like servlets and can keep in the web application server as HTML file. The web application server in turn compile the java code in JSP and load it in its library for future execution. Servlet can be perceived as HTML in Java code. The servlet is the class file, which would be loaded in the web application server as a program. The program output will be directed to the outstream object which in turn direct to the client as HTML elements.


What is servlet mapping?

A Servlet Mapping is a directive in the web.xml that tells the Servlet Container which class to use when a particular Servlet is called. A Servlet is a class within your Java Web Application. Let's say you have a servlet called MyServlet in the com.example.servlet package. You would need to have a Servlet Mapping pointing the path "/MyServlet" to the "com.example.servlet.MyServlet" class. Without the servlet mapping, you would not be able to invoke your servlet because the Servlet container would not know where it is. JSPs are different - they do not need mappings like this. JSPs exist within the WebRoot of the application, so they are always available. Servlets exist in the WEB-INF\Classes directory once your application is deployed.


How do you get the image file from the database with JSP?

You can retrieve an image file from a database in JSP by writing a servlet that fetches the image from the database and streams it to the JSP page. The servlet will set the content type to &quot;image/jpeg&quot; or the appropriate image format and write the image data to the response output stream. In the JSP page, you can then display the image by setting the source attribute of the img tag to the servlet URL.


What is servlet?

server side program or single instance multiple threads.


Session tracking in servlet?

Each web application contains a Session that can contain data that can be commonly made available to all the parts of the web application. The Session can be accessed from both the JSP and the Servlet. Ex: The servlet contains references to the HttpRequest object. So to access the session you can do so as below: HttpSession session = request.getSession();


What the use of java servlet?

Servlets serve as the central point of control of a J2EE application. It contains all the processing and control logic of the application.


How easily does servlet integrate into an existing Java application or applet?

(This is a bit subjective). Servlets can integrate easily into an existing Java application or applet, simply because a servlet is just another class, which is a fundamental concept taught to almost every java programmer.


Importance of deployment descriptor in servlet?

The deployment descriptor is an xml file that contains the basic and most important information that is required to deploy a web application (Servlet) Without this, the web server would not know, which requests to entertain/consider as requests to access this servlet.


Why are servlets more secured?

It is very hard to edit a servlet program using hacking.


How many interfaces in a java servlet package?

The javax.servlet package defines 12 interfaces, 7 classes, and 2 exceptions. The Interfaces are as follows:&bull; Filter:- Preprocessor of the request before it reaches a servlet. It can also be a postprocessor of the response leaving a servlet. It can modify a request or response (for example, change headers), the request to a resource (a servlet or static content), or the response from a resource.&bull; RequestDispatcher:- This is the servlet version of a redirect. It enables requests to be processed and then forwarded to other components of a Web application, such as another servlet, HTML file, or JSP file.&bull; Servlet:- Defines the life-cycle methods that are implemented by all servlets.&bull; ServletConfig:- This class has the methods for accessing the servlet configuration information such as the servlets name (from the web.xml file), the initialization parameters, and the ServletContext object.&bull; ServletContext:- These methods enable your servlet to communicate with its servlet container. This is how you get the MIME type of a file, dispatch requests, or write to a log file. Notice that this information has application scope. The most important features of the ServletContext are application-scope attributes access, logging, and context initialization parameters.&bull; ServletContextAttributeListener:- Implementations of this interface receive notifications of changes to the attribute list on the servlet context of a Web application. Supports the handling of the ServletContextAttributeEvent class.&bull; ServletContextListener:- An interface that supports the handling of the ServletContextEvent class. Defines a set of methods that a servlet uses to communicate with its servlet container. It can get the MIME type of a file, dispatch requests, or write to a log file. Notice that there is one context per "Web application" per Java Virtual Machine. The specification defines a "Web application" as a collection of servlets and content installed under a specific subset of the server's URL namespace, such as /catalog, and possibly installed via a .war file.&bull; ServletRequest:- This interface forms the base for the class that provides client request information to a servlet. It is protocol-independent.&bull; ServletResponse:- This interface forms the base for the class that represents the response sent from the servlet to the client.&bull; SingleThreadModel:- An interface that ensures a given servlet handles only one request at a time.