Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

servlet problem (500 server error, no such servlet)

Status
Not open for further replies.

vasah20

Programmer
Feb 16, 2001
559
US
I'm running JRun 3.0, on Win2K and I'm having a problem getting this servlet to run. I know I've put the servlet in the right directory, since I was able to successfully run a simple "Hello World" servlet.

I'm assuming there's something wrong with my servlet specific code, since the main method I've written works with no problems. (the main method is just cut and paste from the doGet, but without the javax.servlet classes)

here's the code, I really hope you can help me:
Code:
/**
 * CCTAdmin.java
 * Used as a wrapper class, to allow
 * users with priviledges to authenticate against a DB and 
 * modify information contained within. The class handles all requests
 * made for administration
 **/

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

class CCTAdmin extends HttpServlet {
	private Connection c = null;
	private Statement s = null;

	public void doPost(HttpServletRequest req, HttpServletResponse res) 
		throws ServletException, IOException {
		
		//first we authenticate the session against what's in the DB
		HttpSession sess = req.getSession();
		if(sess.getAttribute("login.userName") != null) { //1st layer of verification
			res.setStatus(res.SC_UNAUTHORIZED); //not valid, page not found
			return;
		} //end if

		String sql = "SELECT userid FROM CCT_AuthUsers WHERE UserLogin = '" +
			sess.getAttribute("login.userName") + "' AND sid = '" + sess.getId() + "'";


		try {
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			c = DriverManager.getConnection(
					"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:/Site Mirror/test.mdb",
					"","");
			s = c.createStatement();
			ResultSet rs = s.executeQuery(sql);

			if(!rs.next()) {
				res.setStatus(res.SC_UNAUTHORIZED);
				return;
			} //end if

			//once we get here, we're validated and authorized
			int atype = Integer.parseInt(req.getParameter("atype"));

		} catch (ClassNotFoundException e) {
			System.out.println("Class not found!\n" + e.getMessage() + "\n" + e.toString());
		} catch (SQLException e) {
			System.out.println("SQL Error occurred!\n" + e.getMessage() + "\n" + e.toString());
		} finally {
			try {
				if(c != null) c.close(); 
			} catch (Throwable t) { 
				System.out.println("Could not close connection"); 
			} //end try
		} //end try
	} //end doPost

	public void doGet(HttpServletRequest req, HttpServletResponse res) 
		throws ServletException, IOException {
		doPost(req,res);
	} //end doGet

	/**
	 * to test features
	 **/
	public static void main (String [] args) {
		CCTAdmin c = new CCTAdmin();
		c.dodb();
	} //end main

	private void dodb() {
		Connection ct = null;
		Statement st = null;
		//first we authenticate the session against what's in the DB
		String sql = "SELECT userid FROM CCT_AuthUsers WHERE UserLogin = '" +
			 "leo" + "' AND sid = 12";


		try {
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			ct = DriverManager.getConnection(
					"jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:/Site Mirror/test.mdb",
					"","");
			st = ct.createStatement();
			ResultSet rs = st.executeQuery(sql);

			if(!rs.next()) {
				System.out.println("invalid");
				return;
			} //end if

			//once we get here, we're validated and authorized
			int atype = Integer.parseInt("12");

		} catch (ClassNotFoundException e) {
			System.out.println("Class not found!\n" + e.getMessage() + "\n" + e.toString());
		} catch (SQLException e) {
			System.out.println("SQL Error occurred!\n" + e.getMessage() + "\n" + e.toString());
		} finally {
			try {
				if(ct != null) ct.close(); 
			} catch (Throwable t) {} //end try
		} //end try
	} //end main

} //end CCTAdmin
leo

------------
Leo Mendoza
lmendoza-at-garbersoft-dot-net
 
If you get a 500 error it because you don't link properly to your servlet. You need to map it in your web.xml file which should be put in the WEB-INF directory of your application (the parant directory of the classes directory in which your servlet code is). In this file you need to write something like:

<servlet-mapping>
<servlet-name>theNameToDisplayInTheBrowser</servlet-name>
<servlet-class>package.package.CCTAdmin</servlet-class>
</servlet-mapping>

when you have done this you can link to your servlet via:

Else you need to reference it by its full package name:
I don't see (in your code) that your servlet is in a package
and if it is not you need to put the class file directly in the classes directory. If you have it in at sub directory you need to put the CCTAdmin in a package of the same name!!! like (if its in a directory called adminservlets)
include the line
package adminservlets;
before your imports.

Hope thats the problem - Nebse
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top