Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Java Web application: Unable to launch Servlet class from jsp page

  1. #1
    Junior Member
    Join Date
    Apr 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Web application: Unable to launch Servlet class from jsp page

    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
    mvn archetype:generate -DgroupId=com -DartifactId=JavaWebApplication -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeVersion=1.0 -DinteractiveMode=false
    2. 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
    3. I have added servlet dependency in 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>

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java Web application: Unable to launch Servlet class from jsp page

    Try asking the question on this forum: https://coderanch.com/f/56/Tomcat
    There are more programmers there that know Tomcat.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Web application: Unable to launch Servlet class from jsp page

    Quote Originally Posted by Norm View Post
    Try asking the question on this forum: https://coderanch.com/f/56/Tomcat
    There are more programmers there that know Tomcat.
    Thank you for your guidance.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java Web application: Unable to launch Servlet class from jsp page

    Follow here: https://coderanch.com/t/741699/java/...-Unable-launch
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Android Application wont launch on AVD in Eclipse
    By MacM02 in forum Android Development
    Replies: 3
    Last Post: May 7th, 2014, 06:30 AM
  2. maven Application dosen't work , error: Servlet.init() for servlet
    By vector_ever in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 24th, 2013, 04:12 PM
  3. Java Servlet Lotto Application
    By princessfi92 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 8th, 2012, 05:33 PM
  4. Replies: 1
    Last Post: January 13th, 2012, 09:02 AM
  5. Unable to launch app of java application
    By sharmaneelam in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 19th, 2010, 08:42 AM