Solved

Running Java Servlet inside Eclipse Luna

Posted on 2015-01-09
6
157 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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 38
servlet requestdispatcher include and forward differences 1 30
tomcat not starting 6 45
Java Restore security prompts not working 10 12
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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 …

777 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