0

I am creating a simple Java web application "Registration Form". I am using following hardware and softwares: Machine: Amazon Linux 2 EC2 Instance. Java version: 1.8.0_282 Maven: 3.6.3 Apache Tomcat: 7.0.76 Servlet: 3.1.0 (In pom.xml group ID: javax.servlet, artifactId: javax.servlet-api) Note: I am not using any IDE. I am writing code using Linux vim utility.

I am following below steps in order to create Java web application project "Registration Form".

  1. Create project directories and pom.xml using
mvn archetype:generate -DgroupId=com -DartifactId=JavaWebApplication -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeVersion=1.0 -DinteractiveMode=false
  1. Directory structure looks like below:
/home/ec2-user/javaProjects/JavaWebApplication
---src
------main
---------resources
---------java
------------com
---------------JavaWebApplication
------------------Beans
------------------Model
------------------Controller
---------------------guru_register.java
---------webapp
------------css
---------------myStyle.css
------------jsp
---------------register.jsp
------------WEB-INF
---------------web.xml
------------index.jsp
---pom.xml
  1. I have added servlet dependency in pom.xml
  2. Write code in pom.xml, index.jsp, myStyle.css, register.jsp, guru_register.java, web.xml. Code is shown below.
  3. When I execute "mvn clean package" it build the project successfully. After deployment to Tomcat server it display the register.jsp page correctly. After fill data and press submit button it throws error "HTTP Status 404 - /JavaWebApplication/jsp/guru_register". I do not understand why it is looking guru_register page in /jsp folder. Can you please see the code and help me to find the issue?

Note: Due to some limitation I cannot use Windows OS and any IDE so I am using Amazon Linux EC2 for practice. Thank you!

Code:

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com</groupId>
  <artifactId>JavaWebApplication</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>JavaWebApplication Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.1</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>JavaWebApplication</finalName>
  </build>
</project>

index.jsp

<html>
<tittle>Java Web Application</tittle>
<header>
        <link rel="stylesheet" type="text/css" href="css/myStyle.css">
</header>
<body>
        <ul>
                <li><a href="http://23.20.111.62:8080/JavaWebApplication/jsp/register.jsp" class="active">SignUp</a></li>
                <li><a href="#news">SignIn</a></li>
        </ul>
</body>
</html>

myStyle.css

ul {
        list-style-type: none;
        margin: 0;
        padding: 0;
        overflow: hidden;
        background-color: #333;
}
 
li {
        float: left;
}
 
li a {
        display: inline-block;
        color: white;
        text-align: center;
        padding: 14px 16px;
        text-decoration: none;
}
 
li a:hover {
        background-color: #111;
}
 
.active {
        background-color: red;
}

register.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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>Registration Form</title>
</head>
<body>
<h1>Register Here</h1>
<form method="post" action="guru_register">
                        <table style="with: 50%">
                                <tr>
                                        <td>First Name</td>
                                        <td><input type="text" name="first_name" /></td>
                                </tr>
                                <tr>
                                        <td>Last Name</td>
                                        <td><input type="text" name="last_name" /></td>
                                </tr>
                                <tr>
                                        <td>UserName</td>
                                        <td><input type="text" name="username" /></td>
                                </tr>
                                        <tr>
                                        <td>Password</td>
                                        <td><input type="password" name="password" /></td>
                                </tr>
                                <tr>
                                        <td>Address</td>
                                        <td><input type="text" name="address" /></td>
                                </tr>
                                <tr>
                                        <td>Contact No</td>
                                        <td><input type="text" name="contact" /></td>
                                </tr></table>
                        <input type="submit" value="Submit" /></form>
</body>
</html>

guru_register.java

package com.JavaWebApplication.Controller;
 
import java.io.IOException;
 
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 * Servlet implementation class guru_register
 */
public class guru_register extends HttpServlet {
        private static final long serialVersionUID = 1L;
 
     public guru_register() {
             super();
     }
 
   /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.getWriter().append("Served at: ").append(request.getContextPath());
  }
 
 
  /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   */
     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                doGet(request, response);
                //PrintWriter out = response.getWriter();
                // TODO Auto-generated method stub
                String first_name = request.getParameter("first_name");
                String last_name = request.getParameter("last_name");
                String username = request.getParameter("username");
                String password = request.getParameter("password");
                String address = request.getParameter("address");
                String contact = request.getParameter("contact");
 
                System.out.println(first_name);
                System.out.println(last_name);
                System.out.println(username);
 
        }
 
}

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
 
<web-app>
  <servlet>
    <servlet-name>guru_register</servlet-name>
    <servlet-class>com.JavaWebApplication.Controller.guru_register</servlet-class>
  </servlet>
 
  <servlet-mapping>
    <servlet-name>guru_register</servlet-name>
    <url-pattern>/guru_register</url-pattern>
  </servlet-mapping>
 
  <display-name>Archetype Created Web Application</display-name>
</web-app>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555

0 Answers0