1 Reply Latest reply on Sep 25, 2002 7:49 AM by Juha Lindfors

    JBoss Crashed

    Khoa Nguyen Newbie

      Hi,
      here are my files:

      index.html
      --------



      CustomerServlet





      Login : <INPUT TYPE=text NAME=LoginID>
      Password : <INPUT TYPE=password NAME=Password>


      press Submit to post to servlet CustomerServlet





      ----------------

      servlet: CustomerServletClass.java
      ------
      import javax.servlet.*;
      import javax.servlet.http.*;
      import java.io.*;
      import java.sql.*;
      import java.util.*;


      public class CustomerServletClass extends HttpServlet {
      static final private String CONTENT_TYPE = "text/html";
      private Connection connection=null;
      private Statement statement=null;
      StringBuffer logBuffer = new StringBuffer();

      //Initialize global variables

      public void init() throws ServletException {
      try {
      // SQLException, ClassNotFoundException, InstantiationException
      // Load the MM JDBC driver
      // The newInstance() call is a work around for some
      // broken Java implementations
      // Class.forName("org.gjt.mm.mysql.Driver").newInstance();

      Class.forName("com.mysql.jdbc.Driver");

      // since we don't want our database account and password to
      // appear in the program listing, we read it from a private file
      // students should create their own .mysql-init in their top-
      // level directory.

      /* String account="**";
      String password="**";
      try {
      BufferedReader in = new BufferedReader(new InputStreamReader(
      new FileInputStream("/usr/faculty/schmitt/.mysql-init")));
      account=in.readLine();
      password=in.readLine();
      }
      catch (Exception e) {
      System.out.println("StocksMy: cannot read .mysql-init");
      System.exit(1);
      }
      */

      // Once the driver is registered, a Connection can be established.
      // Obtaining a Connection requires a URL for the database.
      // This URL is constructed using the following syntax, where items
      // contained in sqaure brackets are optional:
      // jdbc:mysql://[hostname][:port]/dbname[?param1=value1][=value2]...
      // Create a Connection and a Statement

      connection=DriverManager.getConnection
      // ("jdbc:mysql://proserv:3306/ASIDb?user=huy&password=papi74");
      ("jdbc:mysql://proserv:3306/ASIDb", "huy", "papi74");
      }
      catch (Exception e) {
      System.out.println("JDBC exception");
      System.exit(1);
      }
      }

      //Process the HTTP Get request

      public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      response.setContentType(CONTENT_TYPE);
      PrintWriter out = response.getWriter();
      out.println("");
      out.println("CustomerServlet");
      out.println("");
      out.println("The servlet has received a GET. This is the reply.");
      out.println("");
      }

      //Process the HTTP Post request

      public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      String LoginID, Password;

      LoginID=request.getParameter("LoginID");
      Password=request.getParameter("Password");

      response.setContentType(CONTENT_TYPE);
      PrintWriter out = response.getWriter();

      if(LoginID.equals("") || Password.equals("")){
      out.println("<H3> Please click the back button and fill in all fields. </H3>");
      out.close();
      return;
      }

      boolean success=checkPassword(LoginID,Password);

      if (success){
      out.println("");
      out.println("CustomerServlet");
      out.println("");
      out.println("The password is good.");
      out.println("\n<H3>Error log this session:</H3>\n");
      out.println(logBuffer.toString());
      out.println("");
      out.println("");
      }
      else{
      out.println("");
      out.println("CustomerServlet");
      out.println("");
      out.println("An error occurred or bad password. Please try again later.");
      out.println("\n<H3>Error log this session:</H3>\n");
      out.println(logBuffer.toString());
      out.println("");
      out.println("");
      }
      }

      public boolean checkPassword(String LoginID, String Password) {
      ResultSet rs=null;

      try {
      statement=connection.createStatement();
      String query = "SELECT LoginID, Password FROM tbLogin";
      log("query = " + query);
      rs = statement.executeQuery(query);

      boolean IDFound=false;
      String s,p = null;
      rs.first();

      do {
      s = rs.getString("LoginID");
      p = rs.getString("Password");

      if (s.equals(LoginID)){
      IDFound=true;
      break;
      }
      }
      while (rs.next());

      if (IDFound){
      if (p.equals(Password)){
      return true;
      }
      }
      else
      return false;

      statement.close();
      rs.close();
      }
      catch (Exception e) {
      System.err.println("Error checking password");
      e.printStackTrace();
      return false;
      }
      return true;
      }

      public void log(String s) {
      s = new java.util.Date().toString() + ": " + s;
      System.err.println(s);
      logBuffer.append(s);
      logBuffer.append("\n");
      super.log(s);
      }

      //Clean up resources

      public void destroy() {
      try {
      connection.close();
      }
      catch(Exception e){
      System.err.println("Problem closing the database");
      }
      }
      }
      ----------------
      web.xml:
      ------
      <?xml version="1.0" encoding="ISO-8859-1"?>

      <!DOCTYPE web-app PUBLIC
      '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN'
      'http://java.sun.com/j2ee/dtds/web-app_2_3.dtd'>

      <web-app>


      <servlet-name>CustomerServletClass</servlet-name>
      <display-name>CustomerServletClass</display-name>
      <servlet-class>CustomerServletClass</servlet-class>

      <servlet-mapping>
      <servlet-name>CustomerServletClass</servlet-name>
      <url-pattern>/CustomerModuleServletApp</url-pattern>
      </servlet-mapping>
      <session-config>
      <session-timeout>30</session-timeout>
      </session-config>
      </web-app>

      -----

      I packed with jar cvf test.war
      and after I deployed...
      I can get to index.html
      and after I submit JBoss 3.2.0 crashed.... do you guys know why? I tried with J2EE, it works fine...
      thanks