Solved

Running Java Servlet inside Eclipse Luna

Posted on 2015-01-09
6
179 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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

Technology Partners: 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

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…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

726 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