0

I am running the following code, Which is html version, It shows the name and age boxes.

<!-- Main.html -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>A First page</title>
</head>
<body>
  <form action="Thanks.jsp" method="get">
    Enter Your Name: <input type="text" name="yourName"><br>
    Enter Your Age&nbsp;&nbsp;&nbsp;&nbsp;:<input type="text" name="yourAge"><br>
    <input type="submit" value="Sumitting">
  </form>
</body>
</html>

but when i run the code of same logic which is a servlet version, it got an error

The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. 

Code as follows :

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
@WebServlet("/Main")
public class Main extends HttpServlet {
  private static final long serialVersionUID = 1L;
  public Main() {
    super();
  }

  protected void doGet(HttpServletRequest request, HttpServletResponse   
    response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();     //Is added
    out.println("<form action=\"http://localhost\" method=\"post\">");
    out.println("Enter Your Name: <input type=\"text\" name " +
      "= \"yourName=\" </input><br>");
    out.println("Enter Your Age&nbsp;&nbsp; : <input " + 
      "type=\"text\" name = \"yourAge=\" </input>");
    out.close();
  }
}

Adding web.xml as requested by @RomanC

<?xml version="1.0" encoding="UTF-8"?>
   <web-app xmlns:xsi="w3.org/2001/XMLSchema-instance" 
    xmlns="xmlns.jcp.org/xml/ns/javaee" 
    xsi:schemaLocation="xmlns.jcp.org/xml/ns/javaee 
    xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0"> 
    
    <servlet> 
        <servlet-name>abc</servlet-name> 
        <servlet-class>yi.Main</servlet-class> 
    </servlet> 
</web-app>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • What is the URL you are hitting to get the response? – KnockingHeads May 04 '21 at 05:42
  • http://localhost:8080/Project/WEB-INF/classes/yi/Main.java – yimin zhou May 04 '21 at 06:26
  • Try hitting http://localhost:8080/main I recommend you to go through how servlets work. The point you are missing is the very basic of servlet. The way you are using the URL is not correct. Also, you are trying to access Main.java from inside classes WEB-INF/classes which says that you need to work on your Java basics as well. Basically, once the Java class is compiled, a .class file is created containing bytecode. In your WAR file, it will WEB-INF/classes/main.class but you cannot access it directly. You will need to access the URL you have specified for the servlet. – KnockingHeads May 04 '21 at 06:31
  • Can you show me your web.xml? – Roman C May 04 '21 at 06:36
  • @Ashish when i run the html code , the url is http://localhost:8080/Project/Main.html it works . – yimin zhou May 04 '21 at 06:50
  • @asheish yes, you are right, but it is a sample code from teacher , he let me run this . so i am confused. – yimin zhou May 04 '21 at 07:07
  • Did you try hitting http://localhost:8080/Project/Main Let me know the HTTP Response Code. You can inspect the network log from browser to get the HTTP Response code or send the GET request to above mentioned URL from Postman. – KnockingHeads May 04 '21 at 07:15
  • You should remove `web.xml` because it's useless. See [this](https://stackoverflow.com/a/23856741/573032) answer. If you use Servlet 4.0 it should be fine, i.e. 4.0 < 5.0. Actually, It's not clear from the code why did you write `HttpServlet` class, if your teacher expect `Thanks.jsp`!? – Roman C May 04 '21 at 17:54

1 Answers1

0

Make Sure To add all the html elements (Tag) in servlet.

Try this:

  protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();     //Is added

        String htmlTag
                = "<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";

        out.println(htmlTag
                + "<html>\n"
                + "<head><title>Reg Form</title></head>\n"
                + "<body bgcolor = \"#f0f0f0\">\n"
                + "<h1 align = \"center\">Regform</h1>\n"
                + "<ul>\n"
                + "<form action=\"http://localhost\" method=\"post\">"
                + "Enter Your Name: <input type=\"text\" name = \"yourName\"</input><br><br>\n"
                + "Enter Your Age&nbsp;&nbsp; : <input type=\"text\" name = \"yourAge\" </input>\n"
                + "</ul>\n"
                + "</body>"
                + "</html>");

        out.close();
}

As you are using @Webservlet Annotation you don't require need to mention servlet Mapping in web.xml but still if you are looking for xml

  <servlet>
        <servlet-name>Main</servlet-name>
        <servlet-class>test.Main</servlet-class>  //test is the Package
    </servlet>
    <servlet-mapping>
        <servlet-name>Main</servlet-name>
        <url-pattern>/Main</url-pattern>
    </servlet-mapping>

Run the Servlet File you will get the exact URL.(Right Click on Servlet and Run File)

Generally it's like this: "http://localhost:8084/NameofYourApplication/Main"

naveen4181
  • 171
  • 1
  • 8