A Simple Java Servlet
Here's the basic servlet code:
package servletexample; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class PrintServlet extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>Mini-servlet test</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1> BIG MESSAGE FROM SERVLET</h1><br><hr>"); out.println("</body>"); out.println("</html>"); out.close(); } }
Now, we need to compile this java file into a binary class:
javac -extdirs path-to-tomcat/lib PrintServlet.java
alternatively
javac -classpath path-to-tomcat/lib/servlet-api.jar PrintServlet.java
We then need to move this class file to the tomcat/webapps/servletexample/WEB-INF/classes folder.
That sets up the java to work. Tomcat (or any web servlet container) needs to know what URLs need to go to what class. This informaion goes into a file called web.xml that sits in the application directory under Tomcat (tomcat/webapps/servletexample/WEB-INF/web.xml):
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" version="2.4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http:/java.sun.com/dtd/web-app_2_3.dtd"> <servlet> <servlet-name>servletexample</servlet-name> <servlet-class>servletexample.PrintServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>servletexample</servlet-name> <url-pattern>/Hello</url-pattern> </servlet-mapping> </web-app>Give the servlet a try:
http://localhost:8080/servletexample/Hello
Want to add form posting abilities? There are two ways: POST and GET. Normally, you'd send a form via a POST rather than a GET as GET is for retrieving documents and POST is for 'posting' or sending information. In this example, both work and are virtually identical. If you want to use GET change the method="POST" below to method="GET". Using POST, change the doGet method in the PrintServlet class above to doPost.
Add a web file to the project (in the main WebContent under a standard build or at the same level as the WEB-INF directory) for the form. Notice the use of Hello in the action line - this references the url-pattern in the web.xml file. Here's the html :
<!DOCTYPE html><html><head> <title>A Simple Form Example </title> </head><body> <h1> Send this form! (using POST although it will work with GET)</h1> <form action="Hello" method="POST"> Enter something here: <INPUT type="text" name="name" /> <INPUT type="submit" name="Submit this" value="Submit text string"> </form></body></html>For a POST, add this to the doPost method (or for GET, to doGet):
if (request.getParameter("name") != null){ out.println("<h2>More info - this time from the form:" + request.getParameter("name") + "</h2>"); }
Previously, we'd needed another file in tomcat's conf directory (conf/Catalina/localhost/) called servletexample.xml. In this example, I didn't need it, but had done in previous examples.
<Context path="/servletexample" docBase="C:\apache-tomcat-6.0.18\webapps\servletexample" reloadable="true"> </Context>
Now, if I add it there are complaints in the log files (catalina.out), but the app will load anyway.
Comments
Post a Comment