Wednesday 26 June 2013

Eclipse donot generate a web.xml while creating dynamic web project

This is beacuse you have'nt check the checkbox for automatically generating the web.xml while creating dynamic web project

1. select File-->New-->Dynamic Web Project
2. After entering the name for your project, click next and next again to get to the "web module" window
3. Click the checkbox "generate web.xml deployment descriptor"

Creating servlet in eclipse with Tomcat

1. Create Dynamic Web Project
   Select from the menu File --> New --> Dynamic Web Project.
  

 2. Check 'Generate web.xml deployment descriptor' checkbox and click "Finish" button and Eclipse IDE will  generate the web project  automatically as shown below




3. Project "helloworld" is created.'


4. You need to add the Servlet API to your classpath.
    refer http://lekshmideepu.blogspot.in/2013/06/the-import-javaxservlet-cant-be-resolved.html
5. Create a Servlet Class
    Create a package under src in your project (say com.hello)
     Right click the package, select new-->servlet.(say HelloWorldServlet as class name).
     Click next and finish

6. Add the below code in 'doGet' method of HelloWorldServlet

          response.setContentType("text/html");
          PrintWriter printWriter  = response.getWriter();
          printWriter.println("<h1>Hello World!</h1>");

         So your HelloWorldServlet  looks like below

package com.hello;

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 HelloWorldServlet
 */
@WebServlet("/HelloWorldServlet")
public class HelloWorldServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * Default constructor.
     */
    public HelloWorldServlet() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
          response.setContentType("text/html");
          PrintWriter printWriter  = response.getWriter();
          printWriter.println("<h1>Hello World!</h1>");

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}



7. Create Servlet Mapping in Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
 
  <servlet>
        <display-name>helloworldservlet</display-name>
        <servlet-name>helloservlet</servlet-name>
        <servlet-class>com.hello.HelloWorldServlet</servlet-class>
  </servlet>
  <servlet-mapping>
     <servlet-name>helloservlet</servlet-name>
     <url-pattern>/hello</url-pattern>
  </servlet-mapping>

 
</web-app>

  
9. Add your project to "Tomcat" server and start the server

8. Open a browser and type url:
   http://localhost:8080/helloworld/hello




Friday 14 June 2013

HTTP Status 405 - HTTP method POST is not supported by this URL

Problem:

When i run my servlet by tomcat server, i got error message
HTTP Status 405 - HTTP method POST is not supported by this URL

Solution

The issue is beacause, you made a HTTP post request from jsp form, but you don't have a doPost() method in the servlet to handle it.
you need to add the below line your servlet class.

public void doPost(HttpServletRequest request, HttpServletResponse response)
 throws IOException{
  ....  
 }

Wednesday 12 June 2013

Several ports (8005, 8080, 8009) required by Tomcat Server at localhost are already in use

Issue

When you try to run a jsp program on Tomcat server in eclipse, you are getting the below error in the eclipse console
"Several ports (8005, 8080, 8009) required by Tomcat v6.0 Server at localhost are already in use."

Solution

The issue is because you've another instance of Tomcat already running.
To solve this 
1. Go to bin folder of Tomcat  (eg C:\apache-tomcat-7.0.23\bin )
2. Run startup.bat
3. Run shutdown.bat
4. Start the tomcat from eclipse
 

 

The import javax.servlet can't be resolved

Error : The import javax.servlet can't be resolved in eclipse

Soultion

You need to add the Servlet API to your classpath. In Tomcat 7.0, this is in a JAR called servlet-api.jar in Tomcat's lib folder.
steps to add the JAR into your project class path.

Right-click the project, click Properties.
Choose Java Build Path.
Click Add External JARs
Browse to find servlet-api.jar and select it.
Click OK to update the build path.
Clean and build the project