Sunteți pe pagina 1din 3

6. explain the steps involved in executing a servlet with example program.

Ans:

COMPILING AND RUNNING SERVLET PROGRAM Compiling and running a servlet program requires a
Web Server to be installed on your computer. The Web Server contains the necessary classes required
to create a servlet program. The following example illustrate the compilation of java servlet program
with the help of Java Servlet Development Kit 2.0

Program 20.1 HelloWorld.java

import java.io.*;
import javax.servlet.*;
public class HelloWorld extends GenericServlet
{
public void service(ServletRequest request,
ServletResponse response )
throws IOException
{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<B> Hello World <B>");
pw.close();
}
}
Steps
1. After creating the above program save program with name HelloWorld.java and move to the
MS-DOS prompt.
2. Move to the folder where you have java servlet program
3. At the MS-DOS prompt type the path and classpath of java servlet
4. path = G:\Program Files\Java\jdk1.6.0_01\bin
5. set classpath=G:\JSDK2.0\lib\jsdk.jar
6. compile java program as : javac HelloWorld.java
7. copy HelloWorld.class file and paste it to the example folder of JSDK2.0
8. open servlet.properties files in notepad and type mapping to the servlet as:
9. servlet.code.Hello=HelloWorld
10. move to the bin folder of JSDK2.0 start servletrunner program by double clicking
11. it.
12. Start the web browser by double clicking it, and at its address bar type :
13. http://localhost:8080/servlet/HelloWorld
7. Explain about the character extraction function in java with example.

Ans : The String class provides a number of ways in which characters can be extracted from a
String object. Several are examined here. Although the characters that comprise a string
within a String object cannot be indexed as if they were a character array, many of the
String methods employ an index (or offset) into the string for their operation. Like arrays,
the string indexes begin at zero.

1.charAt( )

1. description:
To extract a single character from a String, you can refer directly to an individual character via the
charAt( ) method.
2. Syntax
char charAt(int where)
Here, where is the index of the character that you want to obtain.
charAt( ) returns the character at the specified location.
3. example
class temp
{
public static void main(String...s)
{
String str="Hello";
char ch=str.charAt(2);
System.out.println(ch);
}
}
Output: 1
In above example ch will contain character l. We must take care that the index should not be negative
and should not exceed string length.

2. getChars( )

1. description:
It is used to extract more than one character. Here stringStart and stringEnd is the starting and ending
index of the substring. arr is the character array that will contain the substring. It will contain the
characters starting from stringStart to stringEnd-1. arrStart is the index inside arr at which substring will
be copied. The arr array should be large enough to store the substring.
2. Syntax
void getChars(int sourceStart, int sourceEnd, char target[ ], int targetStart)
3. example
class temp
{
public static void main(String...s)
{
String str="Hello World";
char ch[]=new char[4];
str.getChars(1,5,ch,0);
System.out.println(ch);
}
}

Output:
ello

S-ar putea să vă placă și