GET&POST
GET&POST
When a request comes in for the servlet, the Web Container calls the
servlet's service() method and depending on the type of request
the service() method calls either the doGet() or doPost() method.
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
response.setContentType(“html/text”);
PrintWriter out=response.getWriter();
out.println(“<html><body>”);
out.println(“<h1>HelloServlet</h1>”);
out.println(“</body></html>”);
Copy
Write above code in a notepad file and save it as MyServlet.java anywhere
on your PC. Compile it(explained in next step) from there and paste the class
file into WEB-INF/classes/ directory that you have to create
inside Tomcat/webapps directory.
Web.xml file
<web-app>
<servlet>
<servlet-name>MECS</servlet-name>
<servlet-class> MyServlet </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MECS</servlet-name>
<url-pattern>/ADD</url-pattern>
</servlet-mapping>
</web-app>
Note:<servlet-name> specifies logical name of the servlet
5. Start the Server: Double click on the startup.bat file to start your
Apache Tomcat Server.
Or, execute the following command on your windows machine using RUN
prompt.
C:\apache-tomcat-7.0.14\bin\startup.bat
6. Starting Tomcat Server for the first time: If you are starting
Tomcat Server for the first time you need to set JAVA_HOME in the
Enviroment variable. The following steps will show you how to set it.
Click on New button, and enter JAVA_HOME inside Variable name text
field and path of JDK inside Variable value text field. Click OK to save.