Solved

Running Java Servlet inside Eclipse Luna

Posted on 2015-01-09
6
147 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Securing Jmx Console and web console 2 64
Java - Why doesn't this JFrame work 3 40
Java Timer (static) 9 36
servlet filter example 37 34
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

929 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

12 Experts available now in Live!

Get 1:1 Help Now