0

I am making a login .JSP where on login button it called to login.java(servlet). But JSP is not able to call the servlet file and it gives an error.

Login.JSP

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>login</title>
</head>
<body>
<form action="Login" method="get">
    Enter Username:<input type="text" name="uname"><br>
    Enter Password:<input type="password" name="upass"><br>
    <input type="submit" value="login">
</form>
</body>
</html>

Login.java(servlet file)

package com.login;

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;

/**
 * Servlet implementation class Login
 */
@WebServlet("/Login")
public class Login extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String uname=request.getParameter("uname");
        System.out.print("uname is"+uname);
        String upass=request.getParameter("upass");
        if (uname.equals("meet") && upass.equals("1234")) {
            response.sendRedirect("welcome.jsp");
        }
        else {
            response.sendRedirect("login.jsp");
        }
    }


}

ERROR MESSAGE HTTP Status 404 – Not Found

Type Status Report

Message /Login

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

enter image description here

Meet
  • 164
  • 2
  • 11

1 Answers1

1

Simply you can use getConextPath in jsp request

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
         pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
  <meta charset="ISO-8859-1">
  <title>login</title>
</head>
<body>
<form action="<%=request.getContextPath()%>/Login" method="get">
  Enter Username:<input type="text" name="uname"><br>
  Enter Password:<input type="password" name="upass"><br>
  <input type="submit" value="login">
</form>
</body>
</html>

Wish i was helpfull; but regarding to BalusC using Scriptlets <% ... %> are officially discouraged since JSP 2.0 which was introduced in 2003(!!). Please do not encourage starters to use bad practices. The correct practice is to use EL ${ ... } instead. –

so you can write this way and always try to use it on this way

  <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
             pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="ISO-8859-1">
      <title>login</title>
    </head>
    <body>
    <form action="${requestScope.getContextPath}/Login" method="get">
      Enter Username:<input type="text" name="uname"><br>
      Enter Password:<input type="password" name="upass"><br>
      <input type="submit" value="login">
    </form>
    </body>
    </html>

Its better to know the bad coding and the write coding , wish i this help you too.

  • 1
    Scriptlets `<% ... %>` are officially discouraged since JSP 2.0 which was introduced in 2003(!!). Please do not encourage starters to use bad practices. The correct practice is to use EL `${ ... }` instead. – BalusC Nov 11 '19 at 16:11
  • Yes @BalusC your right, it my fault. – Shant Khayalian Nov 12 '19 at 07:01