Solved

Running Java Servlet inside Eclipse Luna

Posted on 2015-01-09
6
139 Views
Last Modified: 2015-09-11
I have Eclipse IDE with integrated Tomcat, all setup and working. For example this code works fine:

import java.io.IOException;
import java.io.PrintWriter;

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 MyServlet
 */
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public MyServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<html><boby>");
		out.println("<h3>Hello readers</h3>");
		out.println("</body></html>");
	}

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

Open in new window


at URL http://localhost:8080/JavaTpoint/MyServlet

But with this code I can't make it work:
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class First implements Servlet {
	ServletConfig config = null;

	@Override
	public void init(ServletConfig config) {
		this.config = config;
		System.out.println("servlet is initialized");
	}

	@Override
	public void service(ServletRequest req, ServletResponse res)
			throws IOException, ServletException {

		res.setContentType("text/html");

		PrintWriter out = res.getWriter();
		out.print("<html><body>");
		out.print("<b>hello simple servlet</b>");
		out.print("</body></html>");

	}

	@Override
	public void destroy() {
		System.out.println("servlet is destroyed");
	}

	@Override
	public ServletConfig getServletConfig() {
		return config;
	}

	@Override
	public String getServletInfo() {
		return "copyright 2007-1010";
	}

}

Open in new window


Eclipse opens this URL
http://localhost:8080/JavaTpoint/WEB-INF/classes/First.java

Open in new window


with this info
HTTP Status 404 -
type Status report
message
description The requested resource is not available.
Apache Tomcat/7.0.54

Even if I try to enter different URL  
http://localhost:8080/JavaTpoint/First

Open in new window

it does the same.

What I am doing wrong here and why is Eclipse not opening
http://localhost:8080/JavaTpoint/First
instead of
http://localhost:8080/JavaTpoint/WEB-INF/classes/First.java
0
Comment
Question by:KPax
  • 3
  • 3
6 Comments
 
LVL 27

Assisted Solution

by:rrz
rrz earned 500 total points
ID: 40540589
A browser doesn't have access to anything within the WEB-INF folder.
But, if you add the lines
import javax.servlet.annotation.WebServlet;  
and  
@WebServlet("/First")  
to your second Servlet, then you should be able to browse to
http://localhost:8080/JavaTpoint/First
0
 

Author Comment

by:KPax
ID: 40540937
Unfortunately, even if I add @WebServlet("/First")  so class declaration look like

@WebServlet("/First")
public class First implements Servlet {
	ServletConfig config = null;

Open in new window


on Run Eclipse opens same URL as before: http://localhost:8080/MyProject2/WEB-INF/classes/First.java
0
 
LVL 27

Assisted Solution

by:rrz
rrz earned 500 total points
ID: 40541033
Can't you just type into your browser's address bar?
http://localhost:8080/MyProject2/First
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:KPax
ID: 40541606
Yes, I can, it works that way, but why
@WebServlet("/First")
does not open the right URL i.e. http://localhost:8080/MyProject2/First instead of http://localhost:8080/MyProject2/WEB-INF/classes/First.java ?
0
 
LVL 27

Accepted Solution

by:
rrz earned 500 total points
ID: 40542096
I don't know why it doesn't work the way you want it to work. I tried to change the run configuration on my Eclipse, but I was unsuccessful.  The closest I could come was to add a link on my welcome page.  
<a href="/MyProject2/First">go to First</a>  

I noticed that at the very end of the following tutorial  
http://www.codejava.net/ides/eclipse/how-to-create-deploy-and-run-java-servlet-in-eclipse    
the author  does type the URL into the address bar. Maybe that is the only way. Or maybe some expert that is listening here will show us how it is done.
0
 

Author Comment

by:KPax
ID: 40542308
Yes that is the tutorial I was working with. If no one else answers this in few days, I will request attention.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now