0

I'm getting an error in my browser when trying to run a servlet. It says

The requested resource is not available.

and

"HTTP Status 404-". The console doesn't print any errors but one warning: " [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:CDM' did not find a matching property."

It feels like I've tried everything to get it to work so I'm open to any suggestion that will make my code work.

StudentResultRegistered.java

package com.cdm;


import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;




@WebServlet(name="studentResultRegistered",urlPatterns={"/herpDerp"})
public class StudentResultRegistered extends HttpServlet {



    @Override
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    String s=request.getParameter("studentID");  
    String k=request.getParameter("kursKod");  
    String t=request.getParameter("termin");  
    String p=request.getParameter("provNr"); 
    String b=request.getParameter("betyg"); 



    try{
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/school_server", "testuser", "testuser");



        String sql = "INSERT INTO regStudentResult (studentID, kursKod, termin, provNr, betyg)" +
                "VALUES (?, ?, ?, ?, ?)";

        PreparedStatement ps = con.prepareStatement(sql);

        ps.setString(1,s);  
        ps.setString(2,k);  
        ps.setString(3,t);  
        ps.setString(4,p);
        ps.setString(5,b);

        int i=ps.executeUpdate();  
        if(i>0)  
        out.print("success");  
    }catch (Exception e2) {System.out.println(e2);}  

    out.close();  
    } 


}//studentResultRegistered

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Course Data Management</title>
</head>
<body>
<h1>Registrera Resultat</h1>

<FORM ACTION="/studentResultRegistered" method="get">
  StudentID:
  <INPUT TYPE="TEXT" NAME="studentID" VALUE=""><BR>

  Kurskod:
  <INPUT TYPE="TEXT" NAME="kursKod" VALUE=""><BR>

  Termin:
  <INPUT TYPE="TEXT" NAME="termin" VALUE=""><BR>

  ProvNr:
  <INPUT TYPE="TEXT" NAME="provNr" VALUE=""><BR>

  Betyg:
  <INPUT TYPE="TEXT" NAME="betyg" VALUE=""><BR>


    <INPUT TYPE="SUBMIT" VALUE="Registrera Resultat">
</FORM>

</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>CDM</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>  
  </welcome-file-list>



 <servlet>
    <servlet-name>studentResultRegistered</servlet-name>
    <servlet-class>com.cdm;</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>studentResultRegistered</servlet-name>
    <url-pattern>/herpDerp</url-pattern>
</servlet-mapping>

</web-app>
bork
  • 1,556
  • 4
  • 21
  • 42

2 Answers2

2

I think you need a full class name not just a package:

<servlet-class>com.cdm.StudentResultRegistered</servlet-class>

EDIT:

Additional problems to be fixed:

  1. index.html should be placed in the web folder (parent of WEB-INF)
  2. should be accessed as http://localhost/index.html or http://localhost/CDM/index.html depending on how the web context is configured (see deploy webapp from Eclipse to Tomcat root context). Note that index.html is not required in the url, as it is already specified as a default page in web.xml
  3. Action form url should match the servlet path - not the servlet name.
Community
  • 1
  • 1
yurgis
  • 4,017
  • 1
  • 13
  • 22
  • changed it, no difference same error:( – bork Jan 11 '15 at 22:29
  • Are you getting the error on clicking submit? Your form action should match the url /herpDerp - not the servlet name – yurgis Jan 11 '15 at 22:38
  • I don't get as far as seeing my form due to the "The requested resource is not available."-error. I.e. I get the error when trying to run the servlet from the beginning – bork Jan 11 '15 at 22:41
  • I see, this message is about not being able to find your index.html resource. I assume your tomcat runs on port 80. You need to put index.html inside web directory (parent of WEB-INF). Then try http://localhost/CDM/index.html. Also if that does not work try http://localhost/index.html – yurgis Jan 11 '15 at 22:48
  • you, sir or madam, just saved my sanity! thanks :D – bork Jan 11 '15 at 22:57
  • You're welcome. Please don't forget to fix your form action as I mentioned above, and mark the answer accepted if it works for you. :) – yurgis Jan 11 '15 at 23:03
  • Thanks alot, I had my index.jsp inside web-inf folder – pnv Feb 21 '17 at 10:38
0

Just saw your comment saying the URL in your browser is

localhost/CDM/WEB-INF/index.html

WEB-INF is a hidden directory - Tomcat (or any other Servlet container) will never serve content from that directory, it should contain confirguation, classes and jars.

Your index.html file should be in the application root (in CDM directory) and your URL should be

localhost/CDM/index.html
NickJ
  • 9,380
  • 9
  • 51
  • 74