www.webdeveloper.com
Results 1 to 4 of 4

Thread: Tomcat 404 on servlet request

  1. #1
    Join Date
    Jul 2009
    Posts
    45

    Tomcat 404 on servlet request

    Hello all,

    I have installed xampp on machine running Windows 7 Home Premium, including tomcat plugin

    They have all installed successfully and all will turn on.

    When visiting http://localhost:8080/ I see a blank white page (but it has the tomcat favicon)

    When running the example from the following website, it works:
    http://www.frank-buss.de/echoservlet/index.html

    I have created a servlet with the following code (it compiles):

    Code:
    package base;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.sql.*;
    
    /**
     * Servlet implementation class FirstServlet
     */
    public class FirstServlet extends HttpServlet {
    	private static final long serialVersionUID = 1L;
    	private String text = "";
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public FirstServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
        
        protected void mysql_connect() {
        	Connection con = null;
            try {
              Class.forName("com.mysql.jdbc.Driver").newInstance();
              con = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "woops forgot to remove this the first time");
    
              if(!con.isClosed()) {
                text = "Successfully connected to MySQL server using TCP/IP...";
              }
            } catch(Exception e) {
              text = "Exception: " + e.toString();
            } finally {
              try {
                if(con != null)
                  con.close();
              } catch(SQLException e) {
            	  text = e.getMessage();
              }
            }
        }
    
    	/**
    	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		mysql_connect();
    		response.setContentType("text/html");
    		PrintWriter out = response.getWriter();
    		out.println("This is my first servlet<br/>");
    		out.println(text);
    	}
    
    	/**
    	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    	}
    
    }
    This class is located in : C:\xampp\tomcat\webapps\Servlet\WEB-INF\classes\base\

    The mysql-connector is in the lib folder of the web application

    This is the web.xml file (which is located in ""\Servlet\WEB-INF):

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
        <servlet>
            <description>FirstServlet</description>
            <servlet-name>FirstServlet</servlet-name>
            <servlet-class>base.FirstServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>FirstServlet</servlet-name>
            <url-pattern>/Servlet</url-pattern>
        </servlet-mapping>
    </web-app>
    However when I visit:
    http://localhost:8080/Servlet/FirstServlet

    I get a tomcat error 404 page stating the requested resource is not available

    As you can probably tell, I'm fairly new with tomcat and java, and don't really understand how it works. If someone could point out what I'm missing I would appreciate it very much.

    EXTRA
    When the tomcat server is started, it automatically configures the mod_jk file to add a jkmount rule to whatever directories are in the webapps folder

    Also, I have another tomcat isntallation in C:\tomcat, which Eclipse is configured to use, if I turn the xampp tomcat server off, and eclipse starts the other server, I can go to:
    http://localhost:8080/Servlet/FirstServlet and it works.

    **EDIT**
    Just thought of something, I had to add another tomcat "installation" because when I tried to get eclipse to use the C:\xampp\tomcat folder it would, I forget exactly why.

    Thanks in advance
    Noah
    Last edited by ntk418; 06-04-2010 at 01:02 PM.

  2. #2
    Join Date
    Jul 2009
    Posts
    45
    Problem solved. Not sure what I did to solve it or I would post the solution. Was just errors on my part.

  3. #3
    Join Date
    Jul 2009
    Posts
    45
    Code:
    <url-pattern>/Servlet</url-pattern>
    to

    Code:
    <url-pattern>/servlet/FirstServlet</url-pattern>
    I believe that was the problem.

  4. #4
    Join Date
    May 2011
    Posts
    1

    Help plz

    Hello,

    im a complete noob in java.

    if i have to modify this program to request a web page from the tomcat server,what codes must i use?


    please help me out..

    even if i have to get the answer from the server as html codes and then display the codes..
    ( i mean,not displaying the actual web page in the java program,but getting the tag like <html><body> etc )

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles