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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Servlet not responding

Status
Not open for further replies.

MarkShark

Programmer
Aug 22, 2001
36
US
I have a servlet that compiled just fine, but still will not call up the data I need to pull up onto a website. I'm totally lost here and would greatly appreciate any help I can get with it. Thanks greatly in advance. It's attached below:

import cmi.xml.AU;
import cmi.xml.Block;
import cmi.xml.CourseReader;
import cmi.xml.CSFElement;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io_ObjectInputStream;
import java.io_ObjectOutputStream;
import java.io.PrintWriter;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServlet;

import org.jdom.JDOMException;

public class LessonServlet extends HttpServlet
{
public void init (ServletConfig config) throws ServletException
{
super.init (config);
/*try {
Class.forName (GlobalData.DatabaseDriverName).newInstance();
_jdbcConnection = DriverManager.getConnection (GlobalData.DatabaseName);

//load serialized course data
loadCourseData();
}
catch (Exception xcp) {
xcp.printStackTrace();
}*/
}

public void doGet( HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
boolean normalMode = true;

try {
HttpSession session = request.getSession (true);
ObjectInputStream in =new ObjectInputStream(request.getInputStream());
//can I load the session given the id??
//System.out.println ("Lesson.valid session " + session.getId() + ": " + request.isRequestedSessionIdValid());

Class.forName (GlobalData.DatabaseDriverName).newInstance();
_jdbcConnection = DriverManager.getConnection (GlobalData.DatabaseName, "tesco", "scorm");

//load serialized course data
loadCourseData();

JDBCHelper dbHelper = new JDBCHelper (_jdbcConnection);

//get student ID
Integer studentID = (Integer) session.getValue ("studentID");
//get course ID
Integer courseID = (Integer) session.getValue ("courseID");
//get lesson ID
String lessonID = request.getParameter ("lessonID");
if (lessonID == null) {
lessonID = (String) session.getValue ("lessonID");
}

if (studentID == null || courseID == null || lessonID == null) {
//reset session data by re-logging the user
sendProfileError (response.getOutputStream());
return;
}

//store lesson ID in session
session.putValue ("lessonID", lessonID);

String auID = request.getParameter ("auID");
String mode = request.getParameter ("mode");
if (mode != null) {
session.putValue ("mode", mode);
}
else {
mode = (String) session.getValue ("mode");
}
if (mode.equalsIgnoreCase ("review")) {
normalMode = false;
}
else {
normalMode = true;
}

//synchronize access to course hash table
synchronized (_courseHash)
{
//make sure _courseHash is in tact
if (_courseHash == null) {
//try reloading it....
loadCourseData();
if (_courseHash == null) {
//error
response.getOutputStream().close();
throw new IOException ("Corrupt course data");
}
}
if (! _courseHash.containsKey (courseID.toString())) {
//try reloading it....
loadCourseData();
if (! _courseHash.containsKey (courseID.toString())) {
//error
response.getOutputStream().close();
throw new IOException ("Corrupt course data (course not found)");
}
}
}

if (auID == null) {
//show course menu
Hashtable hash = (Hashtable) _courseHash.get (courseID.toString());
sendAvailableAUs (hash, studentID.intValue(), courseID.intValue(), lessonID, response.getOutputStream(), response, dbHelper);
return;
}

//if AU has not been attempted, initialize it
Integer auDataID = new Integer (getAUDataID (studentID.intValue(), courseID.intValue(), lessonID, auID, dbHelper));
//if (getAUDataID (studentID.intValue(), courseID.intValue(), lessonID, auID, dbHelper) == -1) {
if (auDataID.intValue() == -1) {
int newID = initializeAUData (studentID.intValue(), courseID.intValue(), lessonID, auID, dbHelper);
dbHelper.addAUToPath (studentID.intValue(), courseID.intValue(), lessonID, auID);
auDataID = new Integer (newID);
}

session.putValue ("AUID", auID);
session.putValue ("AUDataID", auDataID);

sendAU (studentID.intValue(), courseID.intValue(), lessonID, auID, auDataID.intValue(), normalMode, response.getOutputStream(), dbHelper);
}
//to do: detailed messages should be sent to the client depending on which
// exception was thrown. Don't have one try/catch....have one for each situation
catch (Exception e) {
e.printStackTrace();
handleException(response, e);
ObjectOutputStream out =new
ObjectOutputStream(response.getOutputStream());
out.writeObject(e.getMessage());
out.flush();
out.close();
}
}

public void destroy()
{
try {
if (_jdbcConnection != null) {
_jdbcConnection.close();
}
}
catch (Exception ignored) {}
}

private int initializeAUData (int studentID, int courseID, String lessonID, String auID, JDBCHelper dbHelper)
{
String sqlQuery = null;
ResultSet results = null;
try {
//get student's name
sqlQuery = "SELECT Full_Name" +
" FROM " + GlobalData.StudentTable +
" WHERE Student_ID = " + studentID;
results = dbHelper.doQuery (sqlQuery);
if (! results.next()) {
//error
return -1;
}
String studentName = results.getString (1);
results.close();

sqlQuery = "Insert Into " + GlobalData.AUDataTable +
"(Course_ID, Lesson_ID, AU_ID, student_id, student_name, lesson_location, credit," +
" lesson_status, entry, exit, score_raw, score_max, score_min, total_time," +
" session_time, lesson_mode, suspend_data, launch_data, Evaluation_ID, Objective_ID)" +
" Values (" + courseID + ", '" + lessonID + "', '" + auID + "', " + studentID + ", '" + studentName + "'," +
" 'NA', 'credit'," + " 'not attempted', 'ab-initio', " + "'NA', " + 0 + ", " + 0 + ", " + 0 +
", '00:00:00.0', '00:00:00.0', " + " 'normal'" + ", 'NA', " + "'NA', " + 0 + ", " + 0 + ");";
dbHelper.executeUpdate (sqlQuery);

return getAUDataID (studentID, courseID, lessonID,auID, dbHelper);
}
catch (Exception e) {
handleException(response, e);
e.printStackTrace();
}
return -1;
}

private int getAUDataID (int studentID, int courseID, String lessonID, String auID, JDBCHelper dbHelper)
throws SQLException
{
String sqlQuery = "SELECT AUData_ID, lesson_status, lesson_mode, exit" +
" FROM " + GlobalData.AUDataTable +
" WHERE student_id = " + studentID +
" AND Course_ID = " + courseID +
" AND Lesson_ID = " + "'" + lessonID + "'" +
" AND AU_ID = '" + auID + "';";
ResultSet results = dbHelper.doQuery (sqlQuery);
if (results.next()) {
return results.getInt ("AUData_ID");
}
return -1;
}

private void sendAU (int studentID, int courseID, String lessonID, String auID, int auDataID, boolean normalMode, ServletOutputStream htmlOut, JDBCHelper dbHelper)
throws IOException, ClassNotFoundException
{
Hashtable hash = null;
synchronized (_courseHash)
{
hash = (Hashtable) _courseHash.get (String.valueOf (courseID));
if (hash == null) {
loadCourseData();
hash = (Hashtable) _courseHash.get (String.valueOf (courseID));
if (hash == null) {
throw new IOException ("Corrupt course data (course not found)");
}
}
}
AU au = (AU) hash.get (auID);

try {
if (! normalMode) {
dbHelper.setReviewMode (auDataID);
}
String courseFileName = getFileName (String.valueOf (courseID), dbHelper);
File file = new File (courseFileName);
//create absolute path to file so we can resolve relative URLs
//String newFileName = file.getParent() + "\\" + au.getLaunchParams();
//for Unix: use '/' instead of '\'
String newFileName = file.getParent() + "/" + au.getLaunchParams();
BufferedReader buf = new BufferedReader (new FileReader (newFileName));
PrintWriter htmlWriter = new PrintWriter (htmlOut);
String temp;
htmlWriter.write (getAUHtml (au.getLaunchParams()));
htmlWriter.flush();
htmlWriter.close();
}
catch (Exception e) {
handleException(response, e);
e.printStackTrace();
ObjectOutputStream out =new ObjectOutputStream(response.getOutputStream());
out.writeObject (e.getMessage());
out.flush();
pw.close();
}
}

private String getAUHtml (String path){
path = path.replace ('\\', '/');
String response;
response = &quot;<html>\n&quot; +
&quot;<head>\n&quot; +
&quot;</head>\n&quot; +
&quot;<body>\n&quot; +
&quot;<script language=\&quot;JavaScript\&quot;>\n&quot; +
&quot;document.location = \&quot;/cmi/courses/&quot; + path + &quot;\&quot;\n&quot; +
&quot;</script>\n&quot; +
&quot;</body>\n&quot; +
&quot;</html>\n&quot;;
return response;
}

private void sendAvailableAUs (Hashtable hash, int studentID, int courseID, String lessonID, ServletOutputStream out, HttpServletResponse response, JDBCHelper dbHelper)
{
StringBuffer buf = new StringBuffer (200);
Block block = (Block) hash.get (lessonID);
AU au = null;

try {
Vector completedAUs = dbHelper.getCompletedAUs (studentID, courseID, lessonID);
buf.append (&quot;<html>\n&quot; +
&quot;<head>\n&quot; +
&quot;<title>&quot; + block.getTitle() + &quot; units</title>\n&quot; +
&quot;<script language=\&quot;JavaScript\&quot;>\n&quot; +
&quot;function go() {\n&quot; +
&quot; var form = document.goForm;\n&quot; +
&quot; var index = form.gotoSelect.selectedIndex;\n&quot; +
&quot; if (index == 0) {\n&quot; +
&quot; document.location = \&quot;/servlet/CourseServlet?courseID=&quot; + courseID + &quot;\&quot;;\n&quot; +
&quot; }\n&quot; +
&quot; else if (index == 1) {\n&quot; +
&quot; top.restart();\n&quot; +
&quot; }\n&quot; +
&quot;}\n&quot; +
&quot;</script>\n&quot; +
&quot;</head>\n&quot; +
&quot;<body background=\&quot;/cmi/images/marble.jpg\&quot;>\n&quot; +
&quot;<center><h1><b><u>&quot; + block.getTitle() + &quot;</u></b></h1></center>\n&quot; +
&quot;<p></p>\n<p></p>\n&quot; +
&quot;<b>&quot; + block.getTitle() + &quot; contains the following units: </b>\n&quot; +
&quot;<dl>\n&quot;);

Enumeration enum = block.getChildren();
while (enum.hasMoreElements()) {
String temp = (String) enum.nextElement();
CSFElement element = (CSFElement) hash.get (temp);
if (element.getType().equals (&quot;au&quot;)) {
au = (AU) element;
if (completedAUs.contains (au.getID())) {
buf.append (&quot;<dt><p><img src=\&quot;/cmi/images/node2.gif\&quot;><a href=\&quot;/servlet/LessonServlet?lessonID=&quot; + block.getID() + &quot;&auID=&quot; + au.getID() + &quot;&mode=review\&quot;>&quot; + au.getTitle() + &quot; (<i>review</i>) </a></p></dt>\n&quot;);
}
else {
buf.append (&quot;<dt><p><img src=\&quot;/cmi/images/node.gif\&quot;><a href=\&quot;&quot; + response.encodeURL (&quot;/servlet/LessonServlet?lessonID=&quot; + block.getID() + &quot;&auID=&quot; + au.getID() + &quot;&mode=normal&quot;) + &quot;\&quot;>&quot; + au.getTitle() + &quot;</a></p></dt>\n&quot;);
}
}
else if (element.getType().equals (&quot;block&quot;)) {
buf.append (&quot;<dt><p><img src=\&quot;/cmi/images/folder.gif\&quot;><a href=\&quot;&quot; + response.encodeURL (&quot;/servlet/LessonServlet?lessonID=&quot; + element.getID() + &quot;&mode=normal&quot;) + &quot;\&quot;>&quot; + element.getTitle() + &quot;</a></p></dt>\n&quot;);
}
}
buf.append (&quot;</dl>\n&quot; +
&quot;<br><br>\n&quot; +
&quot;<form name=\&quot;goForm\&quot;>\n&quot; +
&quot;<select name=\&quot;gotoSelect\&quot;>\n&quot; +
&quot; <option value=\&quot;lesson\&quot;>Lesson Menu</option>\n&quot; +
&quot; <option value=\&quot;exit\&quot;>Exit LMS</option>\n&quot; +
&quot;</select>\n&quot; +
&quot;<input type=\&quot;button\&quot; value=\&quot;Go\&quot; onClick=\&quot;go()\&quot;>\n&quot; +
&quot;</form>\n&quot; +
&quot;</body>\n&quot; +
&quot;</html>&quot;);
ObjectOutputStream out =new ObjectOutputStream(response.getOutputStream());
out.writeObject(e.getMessage());
out.flush();
out.close();
}
catch (Exception e) {
e.printStackTrace();
}

}
private String getFileName (String courseID, JDBCHelper dbHelper)
throws ClassNotFoundException, SQLException
{
ResultSet results = null;
String fileName = null;
String sqlQuery = &quot;SELECT CourseFile&quot; +
&quot; FROM Course&quot; +
&quot; WHERE Course_ID = &quot; + Integer.parseInt (courseID) + &quot;;&quot;;
results = dbHelper.doQuery (sqlQuery);
if (results.next()) {
fileName = results.getString (1);
}
else {
//need to do more than this :)
System.out.println (&quot;crap!&quot;);
}
results.close();

return fileName;
}

private void loadCourseData()
throws IOException, ClassNotFoundException
{
//load serialized course data
File file = new File (GlobalData.DataFileName);
if (! file.exists()) {
//error
throw new FileNotFoundException (GlobalData.DataFileName + &quot; not found.&quot;);
}
FileInputStream fis = new FileInputStream (GlobalData.DataFileName);
ObjectInputStream ois = new ObjectInputStream (fis);
_courseHash = (Hashtable) ois.readObject();
ois.close();
}

private void sendProfileError (ServletOutputStream out)
{
String html = &quot;<html>\n&quot; +
&quot;<body>\n&quot; +
&quot;<p>An error has occurred with your profile. Please &quot; +
&quot;<a href=\&quot;\&quot; onClick=\&quot;top.restart(); return true\&quot;>login again</a>&quot; +
&quot;</body>\n&quot; +
&quot;</html>\n&quot;;

ObjectOutputStream out =new ObjectOutputStream(response.getOutputStream());
out.writeObject(e.getMessage());
out.flush();
out.close();
}


private Connection _jdbcConnection;
private Hashtable _courseHash;
}
 
Hi, I think it would be quite impossible for anyone to debug through this code without being able to run it. I would suggest that you should try debugging it yourself first and if you encountered more detailed problem, you can ask here.

What you can do is to add some 'System.out.println()' codes in between your servlet to see where it stops, what are the values of the variables at that time etc. You can also view the source code of your output html to see, which part of the programs are 'skipped'.

Best Regards,
Leon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top