CREATE A SIMPLE SERVLET PROGRAM USING HttpServlet import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Hello extends HttpServlet { public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out; String title = "Example of HttpServlet"; // set content type and other response header fields first response.setContentType("text/html"); // then write the data of the response out = response.getWriter(); out.println("<HTML><HEAD><TITLE>"); out.println(title); out.println("</TITLE></HEAD><BODY bgcolor=\"#FFFFFF\">"); out.println("<H1>" + title + "</H1>"); out.println("<H2>Hello WORLD!<br>"); out.println("</BODY></HTML>"); out.close(); } }
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
CREATE A SIMPLE SERVLET PROGRAM USING HttpServlet
import java.io.*;import javax.servlet.*;import javax.servlet.http.*;public class Hello extends HttpServlet{ public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out; String title = "Example of HttpServlet";
// set content type and other response header fields first response.setContentType("text/html"); // then write the data of the response out = response.getWriter(); out.println("<HTML><HEAD><TITLE>"); out.println(title); out.println("</TITLE></HEAD><BODY bgcolor=\"#FFFFFF\">"); out.println("<H1>" + title + "</H1>"); out.println("<H2>Hello WORLD!<br>"); out.println("</BODY></HTML>"); out.close(); }}
OUTPUT:
CREATE A SIMPLE FORM AND DISPLAY THE VALUES USING SERVLET PROGRAM
public class getform extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException {try
< HTML><BODY><% // This scriptlet declares and initializes "date" System.out.println( "Evaluating date now" ); java.util.Date date = new java.util.Date();%>Hello! The time is now<% // This scriptlet generates HTML output out.println( String.valueOf( date ));%></BODY></HTML>
<?xml version="1.0"?><!DOCTYPE address [ <!ELEMENT address (first,middle,last)> <!ELEMENT first (#PCDATA)> <!ELEMENT middle (#PCDATA)> <!ELEMENT last (#PCDATA)>]><address> <first>h-175</first> <middle>first floor,sector-22</middle> <last>noida,u.p.</last></address>
OUTPUT:
EXTERNAL DTD FOR ADDRESS
//store in “add.dtd”
<!ELEMENT address (first,middle,last)><!ELEMENT first (#PCDATA)><!ELEMENT middle (#PCDATA)><!ELEMENT last (#PCDATA)>
//save as xml file
<!DOCTYPE address SYSTEM "add.dtd"><address><first>h-175</first><middle>first floor,sector-22</middle><last>noida,u.p.</last></address>
OUTPUT:
PARSER EXAMPLE
parser.html
<html><body><script type="text/javascript">var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")xmlDoc.async="false"xmlDoc.load("note.xml")document.write("The first XML element in the file contains: ")document.write(xmlDoc.documentElement.childNodes.item(0).text)</script></body></html>
note.xml
<?xml version="1.0" encoding="ISO-8859-1" ?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>OUTPUT:
Steps for Creating a simple bean
Creating a simple Bean Compiling and saving the Bean into a Java Archive
(JAR) file Loading the Bean into the ToolBox Dropping a Bean instance into the BeanBox Inspecting the Bean's properties, methods, and events Generating an introspection report
The Bean will be named SimpleBean.
Here are the steps to create it and view it in the BeanBox: -
Write the SimpleBean code.
Put it in a file named SimpleBean.java, in the directory of the choice. Here's the code:
4. public class SimpleBean extends Canvas5. implements Serializable6. {7. //Constructor sets inherited properties8. public SimpleBean(){9. setSize(60,40);10. setBackground(Color.red);11. }12. }
SimpleBean extends the java.awt.Canvas component. SimpleBean also implements the java.io.Serializable interface, a requirement for all Beans. SimpleBean sets the background color and component size.Make sure the CLASSPATH environment variable is set to point to all needed .class (or .jar) files.
Compile the Bean:
javac SimpleBean.java
This produces the class file SimpleBean.class
Create a manifest file. Use the favorite text editor to create a file, we'll call it manifest.tmp, that contains the following text:
Name: SimpleBean.classJava-Bean: True
Create the JAR file. The JAR file will contain the manifest and the SimpleBean class file:
jar cfm SimpleBean.jar manifest.tmp SimpleBean.class
Load the JAR file into the ToolBox. Select the File|LoadJar... menu item. This will bring up a file browser. Navigate to the SimpleBean.jar location and select it. SimpleBean will appear at
the bottom of the ToolBox. (Note that when the BeanBox is started, all Beans in JAR files in the beans/jars directory are automatically loaded into the ToolBox).
Drop a SimpleBean instance into the BeanBox. Click on the word SimpleBean in the ToolBox. The cursor will change to a crosshair. Move the cursor to a spot within the BeanBox and click. SimpleBean will appear as a painted rectangle with a hatched border. This border means that SimpleBean is selected. The SimpleBean properties will appear in the Properties sheet.
You can resize SimpleBean, because it inherits from Canvas, by dragging a corner. You will see the cursor change to a right angle when over a corner. You can also reposition SimpleBean within the BeanBox by dragging on any non-corner portion of the hatched border. You will see the cursor change to crossed arrows when in position to move the Bean. EXAMPLE OF ACTION ELEMENT: USEBEAN, GETPROPERTY
<%@ page language="java" contentType="text/html" %><html><body color="pink"><jsp:useBean id="c" class="java.util.Date"/>the current DATE AND TIME is <ul><li>Date <jsp:getProperty name="c" property="date"/><li>month<jsp:getProperty name="c" property="month"/><li>year <jsp:getProperty name="c" property="year"/><li>minutes <jsp:getProperty name="c" property="minutes"/><li>seconds <jsp:getProperty name="c" property="seconds"/></ul></body></html>
<html> <body> <p>Each table starts with a table tag. Each table row starts with a tr tag.Each table data starts with a td tag.</p><h4>One column:</h4><table border="1"><tr> <td>100</td></tr></table><h4>One row and three columns:</h4><table border="1"><tr> <td>100</td> <td>200</td> <td>300</td></tr></table> </body> </html>OUTPUT:
<html><head><title>event-onclickonmouseover</title><script language="javascript">function function3(ff){ window.document.n1.v1.value=ff}</script> </head><body><!--THIS IS A COMMENT TAG//--><form name="n1"><input type="text" name="v1" value="jjjjj"> <a href="seven.html" onmouseover='function3("over")' onmouseout='function3("out")'> click </a></form></body></html>OUTPUT:
CREATE A TUTORIAL:
<html><head></head><body><h1>TUTORIAL ON TABLES<h4>a feature common to many j2ee application is for a user to recall information that is stored in database.To store information in efficient manner we use tabls.</body></html>
OUTPUT:
TWO HTML THAT ARE LINKED WITH EACH OTHER
“a.html”<html><head></head><body><a href="b.html"> click here </a></body></html>