Enter your email address:

Delivered by FeedBurner


Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Servlet example for printing random values
11-28-2011, 06:30 PM
Post: #1
Servlet example for printing random values
•Servlets are Java programming language objects that dynamically process requests and construct responses.Here you are going to learn Servlet example for printing random values with Explanation, First write one serlvet program
[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
Find all posts by this user
Add Thank You Quote this message in a reply
Advertise here or at any positions around our site send a mail to info@walkinsforum.com
Post Reply 


[-]
Share/Bookmark (Show All)
Facebook Linkedin Technorati Twitter Digg MySpace Delicious

Possibly Related Threads...
Thread: Author Replies Views: Last Post
Sad how to import a csv file values in to database using jsp rajasri 2 1,214 02-20-2012 12:17 PM
Last Post: rajesh2407
Rainbow Simple Servlet Program mukesh1234 0 316 01-29-2012 08:02 PM
Last Post: mukesh1234
Tongue Creating captcha(random letter image) in jsp rajasri 0 546 12-19-2011 06:09 PM
Last Post: rajasri
Exclamation captcha project in jsp/servlet rajasri 0 598 12-19-2011 05:44 PM
Last Post: rajasri
Heart How to authenticate in a webapplication using jsp and servlet rajasri 0 803 12-13-2011 12:32 AM
Last Post: rajasri
Its My Birthday! How to pass and get values in href as parameters in Jsp rajasri 0 1,270 12-13-2011 12:04 AM
Last Post: rajasri
Big Grin Retrieve database table values in loop using jsp rajasri 0 694 12-12-2011 11:24 PM
Last Post: rajasri
Big Grin HTTP Status 404 - Servlet as is not available rajasri 0 889 12-12-2011 10:15 PM
Last Post: rajasri
Tongue How to run servlet in tomcat 5.5 rajasri 0 1,222 12-12-2011 10:01 PM
Last Post: rajasri
Tongue Servlet example for printing random values rajasri 0 224 12-12-2011 06:48 PM
Last Post: rajasri

Forum Jump:


User(s) browsing this thread: 1 Guest(s)