-1

Here is the hierarchy of a project

It is a Maven Project and I am trying to print hello from a servlet class but it doesn't print.

Here is my web.xml file

<!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>
  <display-name>Json</display-name>
   <servlet>
    <servlet-name>Login_Bridge</servlet-name>
    <servlet-class>com.ebooks.login.main.Login_Bridge</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>Login_Bridge</servlet-name>
    <url-pattern>/Login_Bridge</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

</web-app>

Can anyone figure out where am i going wrong?

Here is my index.jsp class which contain a simple button.

    <html>
<body>
<h2>Hello World!!</h2>
    <form action="/Login_Bridge" method="get"><button>submit</button></form>

</body>

</html>

Code of Login_Bridge

package com.ebooks.login.main;

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;
import java.io.PrintWriter;
/**
 * Servlet implementation class Login_Bridge
 */
@WebServlet("/Login_Bridge")
public class Login_Bridge extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public Login_Bridge() {
        // TODO Auto-generated constructor stub

    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        PrintWriter out=response.getWriter();
        out.println("hello");
        System.out.println("hello");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub


        System.out.println("hello");
    }

}
Mukesh Gupta
  • 1,373
  • 3
  • 17
  • 42
  • you need to paste `Login_Bridge`code too to verify that it does have a get method. – Sabir Khan Dec 20 '15 at 08:42
  • 2
    i have added the code and it contain get method – Mukesh Gupta Dec 20 '15 at 08:46
  • 1
    As per Servlet code `/Login_Bridge` is your URL pattern so implement Massnk Dev's suggestion and it should work or change your URL mapping in XML. – Sabir Khan Dec 20 '15 at 08:51
  • 2
    i have tried that code and it goes to that page but didn't show hello on my page as i described above. – Mukesh Gupta Dec 20 '15 at 08:57
  • 1
    close or flush the stream of [PrintWriter](http://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html#close()). It's better to redirect on new page after submitting a form. Read [http://stackoverflow.com/questions/13621998/how-to-redirect-from-servlet-to-jsp-page](http://stackoverflow.com/questions/13621998/how-to-redirect-from-servlet-to-jsp-page) – Braj Dec 20 '15 at 09:03
  • 2
    I flush the stream but it showing nothing, even i try to printout on my console, it doesn't print. – Mukesh Gupta Dec 20 '15 at 09:11
  • Which URL i didn't get you Abdelhak – Mukesh Gupta Dec 20 '15 at 09:11
  • like localhost:8080.... – Abdelhak Dec 20 '15 at 09:12
  • 1
    http://localhost:8080/IN/ ----> it successfully shows index.jsp but after click on submit it goes to http://localhost:8080/Login_Bridge and print nothing on screen (it should print hello), IN is my project name – Mukesh Gupta Dec 20 '15 at 09:13
  • Is there anything else to do? Because its my first maven project and i am stuck there, i already try same logic with simple Dynamic web project and it runs fine but i don't know why it goes wrong to maven project – Mukesh Gupta Dec 20 '15 at 09:17
  • In your console come printed hello – Abdelhak Dec 20 '15 at 09:35
  • No, even in console it doesn't print hello. – Mukesh Gupta Dec 20 '15 at 09:50
  • Check answer [@WebServlet annotation](http://stackoverflow.com/questions/6535676/webservlet-annotation-with-tomcat-7) – Gurkan Yesilyurt Dec 20 '15 at 10:11
  • I already tried that with no success – Mukesh Gupta Dec 20 '15 at 10:29
  • 1
    @BalusC could you read the question properly, its a maven project and answer you gave its simple servlet project and i already specified that my servlet project running fine but by doing same thing with maven i got error. – Mukesh Gupta Dec 20 '15 at 11:25
  • You placed it in main/resources instead of main/java aka Java Resources. See last sentence of 1st paragraph of the duplicate. – BalusC Dec 20 '15 at 11:26
  • 1
    @BalusC i move my servlet class in main/java and still not able to print hello on my screen, it go to Login_Bridge file but does not print anything – Mukesh Gupta Dec 20 '15 at 12:09
  • 1
    If you have a new question, just ask a new question or search for same questions. Your initial question is already answered. Your servlet is now found and doesn't cause a 404 anymore. – BalusC Dec 20 '15 at 16:30

2 Answers2

1

Try to specific the right url-pattern of servlet here:

  <servlet-mapping>
   <servlet-name>Login_Bridge</servlet-name>
   <url-pattern>/Login_Bridge</url-pattern>
  </servlet-mapping>

Ok try to change this line:

 <form action="Login_Bridge" method="get"><button>submit</button></form>

With:

  <form action="Login_Bridge" method="get"> 
     <input type="submit" value="Submit"/>
  </form>
Abdelhak
  • 8,299
  • 4
  • 22
  • 36
-1
<!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>
  <display-name>Json</display-name>
   <servlet>
    <servlet-name>Login_Bridge</servlet-name>
    <servlet-class>com.ebooks.login.main.Login_Bridge</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>Login_Bridge</servlet-name>
    <url-pattern>/Login_Bridge</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

</web-app>

change your web.xml file with this code and read thislink

and change your jsp with this.

<form action="/IN/Login_Bridge1" method="post"> 
     <input type="submit" value="Submit"/>
  </form>
Community
  • 1
  • 1
Massnk Dev
  • 19
  • 1
  • 6