-1

please before giving me any negative vote. wait... I am very new to this .... and i am unable to solve error...

I am getting the following error on my java servlet first program; Name of servlet class is Anjali and servlet url and name is vashisth.

the error come is this:

HTTP Status 404 - Not Found
type Status report

messageNot Found

descriptionThe requested resource is not available.

GlassFish Server Open Source Edition 5.1.0

the xml code is:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>vashisth</servlet-name>
        <servlet-class>Anjali</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>vashisth</servlet-name>
        <url-pattern>/vashisth</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

the html code is:

<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div>
       <form name="loginForm" method="get" action="Anjali">
    Username: <input type="text" name="username"/> <br/>
    Password: <input type="password" name="password"/> <br/>
    <input type="submit" value="Login" />
</form>

        </div>
    </body>
</html>

and my java program is this:

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Anjali")
public class Anjali extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        // read form fields
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        System.out.println("username: " + username);
        System.out.println("password: " + password);

        // do some processing here...

        // get response writer
        PrintWriter writer = response.getWriter();

        // build HTML code
        String htmlRespone = "<html>";
        htmlRespone += "<h2>Your username is: " + username + "<br/>";      
        htmlRespone += "Your password is: " + password + "</h2>";    
        htmlRespone += "</html>";

        // return response
        writer.println(htmlRespone);

    }

}

Please help me find the error ...tomorrow is the last date of submission of similar assignments... and my whole went rectifying many such types of errors...

please please please

anjali
  • 9
  • 1
  • 3
  • please change method="get" to method="post" in your html code. when you use @WebServlet annotation, web.xml file is really not required. – Govind Jun 06 '20 at 12:19

1 Answers1

0

In you html code you have mentioned method="get" and in your java class Anjali.java you have written code to accept post request only that's why you are getting 404. As you request types are not matching.

Solution:Please change method="get" ---> method="post"

I hope this will solve your problem.

THADI
  • 11
  • 5