| > > > |
Java Servlets Frequently Asked Questions
-
1. Which servlet runner does SVWH use?
-
We're using the JServ Servlet Runner from the Apache group. This is a
top-notch standards based open source project. More information on the
Apache JServ project (including documentation and frequently asked
questions pertaining to JServ) can be found here:
http://java.apache.org/
-
2. How do I add a directory or jar file to
my classpath? How do I add servlet aliases?
-
Directories and jar files can are added to your classpath as
repositories in the zone.properties file (located in your personal
servlet directory). The zone.properties file is well documented; making
changes to this configuration file is easy to do. To add
"/local/home/a0000/code/my_jar_file.jar" to your classpath, simply add
the following line to your zone.properties file:
repositories=/local/home/a0000/code/my_jar_file.jar
Servlet aliases are also added to the zone.properties file. For
example, to alias "SnoopServlet" to "snoop" you would make this entry
in the zone.properties file:
servlet.snoop.com=SnoopServlet
-
3. Where do I upload my servlets and
how do I access them via my browser?
-
In your home directory there will be a subdirectory named "servlet".
Any class files that you upload to that directory will be picked up by
JServ and available for use.
You may add additional directories by adding them as "repositories" to
your zone.properties file (see Question 2).
Once you've uploaded a new class file it will be mapped into your
virtual web server as "/servlet/your_class_file". For example, if you
were to upload "TestServlet.class" to your "/servlet" directory it
would be accessible as follows:
http://www.your_domain.com/servlet/TestServlet
-
4. How do I use Java Server Pages?
-
To make use of Java Server Pages (JSP) simply name your files with a
.jsp extension. JSP is Java's answer to Microsoft's Active Server
Pages. JSP has some significant advantages over ASP.
-
5. How do I use Java HTML pages?
-
To make use of Java HTML pages (JHTML) simply name your files with a
.jhtml extension. Java HTML pages allow you to make use of the
<servlet> tag in your HTML code.
-
6. How do I use the JDBC Drivers?
-
This is a common question that's usually best answered with an example.
When your account was set up an "examples" directory placed in your home
directory. In this directory there are, among other things, examples on
using JSP pages, JHTML pages, and a sample database application written
with JDBC drivers. This sample program illustrates the use of JDBC; a
smaller, more concise example is given below. The below example simply
creates a new table in the "jdbctestdb" database (it's assumed that the
"jdbctestdb" database has already been created).
Further information (including full documentation of the MM JDBC drivers
that are preinstalled) can be found at the MM JDBC Driver web site.
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DBTableTestCreate extends HttpServlet
{
public void doGet (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String login = "YourUsername";
String password = "YourPassword";
String domain = "db.your_domain.com";
String driver = "org.gjt.mm.mysql.Driver";
String database = login + "_jdbctestdb";
String url = "jdbc:mysql://" + domain + ":3306/" + database +
"?autoReconnect=true&maxReconnects=30";
String createString = "CREATE TABLE TableTest " +
"(name varchar(32), id int)";
res.setContentType("text/html");
PrintWriter out = res.getWriter();
try
{
// Load (and therefore register) the MM Driver
Class.forName(driver);
// Get a connection to the database
con = DriverManager.getConnection(url, login, password);
// Create a Statement object
stmt = con.createStatement();
rs = stmt.executeQuery(createString);
out.println(" TableTestCreate");
out.println("");
out.println("Table Created");
out.println("
");
out.println("");
}
catch (ClassNotFoundException e)
{
out.println("Couldn't load database driver: " + e.getMessage());
}
catch (SQLException e)
{
out.println("SQLException caught: " + e.getMessage());
}
finally
{
try
{
if (con != null) con.close();
}
catch (SQLException ignored) { }
}
}
}
-
7. How do I restart my servlet runner?
-
Typically JServ will pick up and automatically reload any changed
class files. However, if you're uploading a large class file through
a relatively slow link (ie. a modem) it is possible for JServ to
reload the class file when it's only partially loaded. If this
happens you may want to restart your personal servlet runner. You
can restart your runner using the Account Controller here:
http://www.svwh.net/support/update.shtml
|
|