|
Servlet example for printing random values
|
|
11-28-2011, 06:30 PM
Post: #1
|
|||
|
|||
|
Servlet example for printing random values
[java] import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class colorservlet extends HttpServlet { public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException { String color=request.getParameter(”color”); response.setContentType(”text/html”); PrintWriter pw=response.getWriter(); pw.println(” <div> <div><strong>The selected color is:”); pw.println(color); pw.close(); } } [/java] Step1: To run servlet You need to compile it first for that you need any of the Jar files given below 1.servlet-api.jar (included in tomcat/lib) Download Tomcat or 2.Jsdk.jar(included in jsdk2.0 or later)Download from sun site After downloading installed it and give classpath for example if you are installing it in the c:/tomcat 5.5/” then you had to assign classpath as set classpath=c:/tomcat5.5/common/lib/servlet-api.jar;and now compile your program asusual like javac colorservlet.java. Step2: To run serlvet you need to create a web.xml file sample web.xml file for helloworld [xml] <web-app> <servlet> <servlet-name>colorservlet</servlet-name> <servlet-class>colorservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>colorservlet</servlet-name> <url-pattern>/colorservlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>colorservlet.html</welcome-file> </welcome-file-list> </web-app> [/xml] Now create colorservlet.html [css htmlscript="true"] <html> <body> <center> <form name=”form1″ method=”get” action=”colorservlet”> <b>Color:</b> <select name=”color” size=”1″> <option value=”red”>Red</option> <option value=”green”>Green</option> <option value=”yellow”>Yellow</option> </select> <br><br> <input type=submit value=”submit”> </form> </body> </html> [/css] Your folder structure will look below colorservlet folder(inside webapps of tomcat installation folder)inside coloservlet folder it look like below. =>WEB-INF=>classes=>class files of servlet =>lib(jar files servlet-api.jar) =>web.xml =>colorservlet.html Step3 : goto http://localhost:8080/colorservlet/ and run the program Happy coding |
|||
Advertise here or at any positions around our site send a mail to info@walkinsforum.com
|
« Next Oldest | Next Newest »
|
![]() |
|||||||
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
|
User(s) browsing this thread: 1 Guest(s)






![[-]](images/royal_red/collapse.gif)






