Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Cannot get Tomcat to work?

Options
  • 30-04-2006 2:39pm
    #1
    Registered Users Posts: 3,809 ✭✭✭


    I have to use tomcat and a servlet to access a database to give data to an applet. I keep getting a "java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost/FinalServe/Serving?date=2006.04.27" message in the java console. I am exhausted trying to get it work but no luck. I have 4 days including today to get this working for my final year project. Any suggestions?


Comments

  • Registered Users Posts: 4,188 ✭✭✭pH


    You're going to have to post the full stack trace of the exception and also the code that throws the exception.


  • Registered Users Posts: 2,800 ✭✭✭voxpop


    check the $TOMCAT_INSTALL_DIR/logs/catilana.out and $TOMCAT_INSTALL_DIR/logs/localhost[date].log


    These log files should give you a better idea of what going on.


    Are you loading any files in your servlet ?


  • Registered Users Posts: 3,809 ✭✭✭CerebralCortex


    http://localhost/FinalServe/
    java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost/FinalServe/Serving?date=2006.04.27
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at HttpMessage.sendGetMessage(HttpMessage.java:35)
    at AppletFinal.graphQuery(AppletFinal.java:111)
    at AppletFinal$1.actionPerformed(AppletFinal.java:86)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)


    I'm not loading any files no, just querying a database using my servlet and then sending it to the applet via a linked list to display data in a chart.


  • Registered Users Posts: 3,809 ✭✭✭CerebralCortex


    the code:
    import java.io.*;
    import java.util.*;
    import java.sql.*;
    import javax.servlet.*;
    import javax.servlet.http.*;

    public class Serving extends HttpServlet {
    //private Connection con;
    private PreparedStatement sqlPrep;


    public void init(ServletConfig config) throws ServletException {
    try {
    Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
    e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }
    try {
    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sensordata", "root",
    "drusslegend");
    // System.out.println(con.getCatalog());
    sqlPrep = connection.prepareStatement("SELECT moteID,temp,packetTime FROM sensordata WHERE error" +
    " LIKE 'clean' AND date LIKE ?");
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
    try {
    String date = req.getParameter("date");
    sqlPrep.setString(1, date);
    System.out.println(date);
    ResultSet resSet = sqlPrep.executeQuery();
    //System.out.println(sqlPrep);
    ResultSetMetaData metaData = resSet.getMetaData();
    int NofC = metaData.getColumnCount();
    LinkedList<String> tempAndtime = new LinkedList<String>();
    while (resSet.next()) {
    for (int i = 1; i <= NofC; i++) {
    tempAndtime.add(String.valueOf(resSet.getObject(i)));
    }
    }
    ObjectOutputStream out = new ObjectOutputStream(res.getOutputStream());
    out.writeObject(tempAndtime);
    // System.out.println(tempAndtime);
    out.flush();
    } catch (SQLException e) {
    e.printStackTrace();
    }
    }
    }

    Appletcode:
    import aw.gui.chart.Chart2D;
    import aw.gui.chart.Trace2DLtd;
    import aw.gui.chart.layout.ChartPanel;

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.util.Properties;
    import java.util.LinkedList;
    import java.net.URL;
    import java.net.MalformedURLException;
    import java.io.InputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;

    /**
    * Created by IntelliJ IDEA.
    * User: Louis Burke
    * Date: 28-Apr-2006
    * Time: 11:23:11
    * To change this template use File | Settings | File Templates.
    */
    public class AppletFinal extends JApplet {
    private JPanel buttonPanel;
    private JButton submitButton;
    private Container cp;
    private JTextField textfield;
    private JLabel tits;
    private Chart2D chart2;
    private Trace2DLtd trace2;
    private Trace2DLtd trace3;
    private ChartPanel chartPanel2;
    private String day;
    private String serPath;
    //private JTabbedPane jtp;

    public void init() {
    cp = this.getContentPane();
    cp.setSize(300, 300);
    this.setLayout(new GridLayout());
    //jtp = new JTabbedPane();
    chart2 = new Chart2D();

    chart2.setGridX(true);
    chart2.setGridY(true);
    // Create an ITrace:
    // Note that dynamic charts need limited amount of values!!!
    trace2 = new Trace2DLtd(30/*, "Mote 1"*/);
    trace3 = new Trace2DLtd(30/*, "Mote 2"*/);

    trace2.setColor(Color.RED);
    trace3.setColor(Color.BLUE);
    trace2.setName("Mote 1");
    //trace2.setPhysicalUnits("Time in millisecomds", "°C");
    trace3.setName("Mote 2");
    //trace3.setPhysicalUnits("Time in millisecomds", "°C");
    trace3.setRenderer(chart2);
    // Add the trace to the chart:
    chart2.addTrace(trace2);
    chart2.addTrace(trace3);
    chart2.setSize(300, 300);
    chartPanel2 = new ChartPanel(chart2);

    buttonPanel = new JPanel();
    buttonPanel.setSize(10, 10);
    textfield = new JTextField(10);
    textfield.setColumns(10);
    buttonPanel.add(textfield);

    submitButton = new JButton("Submit Button");
    tits = new JLabel("In Form yyyy.MM.dd");
    submitButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    // String cmd = e.getActionCommand();
    //
    // if (cmd.equals("Submit")) {
    // System.out.println(textfield.getText());
    char dot = '.';
    String date = textfield.getText();
    // System.out.println(date.charAt(4));
    // System.out.println(date.charAt(7));
    if (date.length() == 10 && date.charAt(4) == dot &&
    date.charAt(7) == dot) {
    try {
    graphQuery(date); //To change body of catch statement use File | Settings | File Templates.
    } catch (IOException e1) {
    e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    } catch (ClassNotFoundException e1) {
    e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }
    }
    }
    // }
    });
    buttonPanel.add(submitButton);
    buttonPanel.add(tits);
    this.add(buttonPanel);
    this.add(chartPanel2);
    }


    public void graphQuery(String date) throws IOException, ClassNotFoundException {
    day = date;
    serPath ="Serving";
    Properties preeps = new Properties();
    preeps.put("date", day);
    //System.out.println(getCodeBase());

    URL url = new URL(getCodeBase(),serPath);
    HttpMessage hm = new HttpMessage(url);
    InputStream in = hm.sendGetMessage(preeps);
    ObjectInputStream ois = new ObjectInputStream(in);
    LinkedList<String> lls = (LinkedList<String>)ois.readObject();

    TimeVTemp tp = new TimeVTemp("Temp Versus Time ", trace2, trace3, lls);
    tp.dataCollect();
    //System.out.println(lls);
    }
    }

    httpmessageclass:
    import java.net.*;
    import java.io.*;
    import java.util.*;

    public class HttpMessage {
    URL servlet = null;
    String args = null;
    public HttpMessage(URL servlet) {
    this.servlet = servlet;
    }
    // Performs a GET request to the previously given servlet
    // with no query string.
    public InputStream sendGetMessage() throws IOException {
    return sendGetMessage(null) ;
    }
    // Performs a GET request to the previously given servlet.
    // Builds a query string from the supplied Properties list.
    public InputStream sendGetMessage(Properties args) throws IOException {
    String argString = ""; // default
    if (args != null) {
    argString = "?" + toEncodedString(args);
    }
    URL url = new URL (servlet.toExternalForm () + argString);
    // Turn off caching
    URLConnection con = url.openConnection();
    con.setUseCaches(false);
    return con.getInputStream();
    }
    // Performs a POST request to the previously given servlet
    // with no query string.
    public InputStream sendPostMessage( ) throws IOException {
    return sendPostMessage(null);
    }
    // Performs a POST request to the previously given servlet.
    // Builds post data from the supplied Properties list.
    public InputStream sendPostMessage(Properties args) throws IOException {
    String argString = ""; // default
    if (args != null) {
    argString = toEncodedString(args); // notice no “?”
    }
    URLConnection con = servlet.openConnection();
    // Prepare for both input and output
    con.setDoInput(true);
    con.setDoOutput(true);
    // Turn off caching
    con.setUseCaches(false);
    // Work around a Netscape bug
    con.setRequestProperty("AbstractDocument.Content -Type",
    "application/x-www-form-urlencoded");
    // Write the arguments as post data
    DataOutputStream out = new DataOutputStream(con.getOutputStream());
    // new JOptionPane(argString);
    out.writeBytes(argString);
    out.flush();
    out.close();
    return con.getInputStream();
    }


    // Converts a Properties list to a URL-encoded query string
    private String toEncodedString(Properties args) {
    StringBuffer buf = new StringBuffer();
    Enumeration names = args.propertyNames();
    while (names.hasMoreElements()){
    String name = (String) names.nextElement();
    String value = args.getProperty(name);
    buf.append(URLEncoder.encode(name) + "=" + URLEncoder.encode(value));
    if (names.hasMoreElements()) buf.append("&");
    }
    return buf.toString();
    }
    }


  • Registered Users Posts: 441 ✭✭robfitz


    Your doing a HTTP GET in the applet.
    public void graphQuery(String date) throws IOException, ClassNotFoundException {
    ...
    InputStream in = hm.sendGetMessage(preeps);
    ...
    }
    

    But you only have doPost handler in the Servlet, you should probably change it to be a service method.
    public void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
    


  • Advertisement
  • Registered Users Posts: 3,809 ✭✭✭CerebralCortex


    Change doPost to a service method?


  • Registered Users Posts: 3,809 ✭✭✭CerebralCortex


    No still getting the same bs.


  • Registered Users Posts: 3,809 ✭✭✭CerebralCortex


    From what I can tell its connecting to the servlet but no I am getting classnotfound exception with regards the database. Any ideas thanks for the help so far.


  • Registered Users Posts: 1,996 ✭✭✭lynchie


    you got the mysql drivers on the classpath in tomcat?


Advertisement