Solved

Running Java Servlet inside Eclipse Luna

Posted on 2015-01-09
6
169 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Configure a Bean in an XML file 4 49
iterator/ListIterator approach 17 55
CSV file parsing thru Java 13 40
Eclipse Java import and method not resolved 4 83
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.

696 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