I am new to Java so I am not sure if I choose the correct forum, but I selected this as per my best understanding. Please forgive me if I made any mistake.
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
2. Directory structure looks like below:mvn archetype:generate -DgroupId=com -DartifactId=JavaWebApplication -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeVersion=1.0 -DinteractiveMode=false
3. I have added servlet dependency in pom.xml/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
4. Write code in pom.xml, index.jsp, myStyle.css, register.jsp, guru_register.java, web.xml. Code is shown below.
5. 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!
I was unable to post below message because of URLs mentioned in it so I removed all URLs.
Code:
pom.xml
<project xmlns="Unable to post message with URL so removed" xmlns:xsi="Unable to post message with URL so removed" xsi:schemaLocation="Unable to post message with URL so removed"> <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>Unable to post message with URL so removed</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.1</version> <scope>test</scope> </dependency> <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="Unable to post message with URL so removed" 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
<percent_symbol at_symbol page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"percent_symbol> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "Unable to post message with URL so removed"> <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; public class guru_register extends HttpServlet { private static final long serialVersionUID = 1L; public guru_register() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().append("Served at: ").append(request.getContextPath()); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); 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 "-FslashFslashSun Microsystems, Inc.FslashFslashDTD Web Application 2.3FslashFslashEN" "Unable to post message with URL so removed" > <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>