Carpetfizz
Carpetfizz

Reputation: 9149

Unable to access Tomcat server from Java servlet

I have a simple Hello World "Dynamic Web Application" written in JavaEE IDE Luna. I have the latest version of Tomcat installed, and I pointed Eclipse to it, by going to the "Servers" tab at the bottom. I added the server, and added my Jar to it. The server started successfully. However, when I go to http://localhost:8080/ I get an HTML page from the server with the warning:

HTTP Status 404 - /

This tells me that the server is running but somehow my doGet() method isn't routed correctly. Here is the code for my doGet().

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws 

    ServletException, IOException {
            response.setContentType("text/html");
            PrintWriter output = response.getWriter();
            output.println("<h1>Hello World</h1>");
        }

Other server programs like webapp2 and node, allow me to specify something like:

app.get('/',function(req,res){//stuff});

but none of the starter tutorials for Servlets mentioned this kind of URL routing.

Thanks for any help!

Upvotes: 0

Views: 298

Answers (1)

user3741687
user3741687

Reputation: 31

you write your web.xml file following content then

<servlet>
     <servlet-name>name</servlet-name>
     <servlet-class>classname</servlet-class>
</servlet>
<servlet-mapping>
     <servlet-name>name</servlet-name>
     <url-pattern>/hello</url-pattern>   //'hello' your url pattern
</servlet-mapping>

put url in browser

localhost:8080/appname/hello

http://howknowledge.com

Upvotes: 2

Related Questions