Js|servlet
What is Servlets?
Servlets is a technology that Java focuses on CGI development. Run on the server side and produce dynamic results. Why use Servlets instead of traditional CGI programs?
Efficiency: Using a traditional CGI program, whenever an HTTP request is received, the system will start a new process to process the request, which can result in lower system performance. The use of the Servlets,java VMs is always running, and when a request is received, the Java VMS create a Java thread to process it immediately, which is much more efficient than starting a new system process each time.
Powerful: Servlets enables you to use many of the powerful features that traditional CGI cannot provide. You can use the Java API to do anything traditional CGI thinks is difficult or impossible. Servlets can easily achieve data sharing and information maintenance, tracking sessions and other functions.
Security: Servlets runs within the bounds of the Servlets engine, just as it can run applets in a Web browser, which helps protect servlets from threats.
Cost: Because Servlets can run on multiple Web servers, you can use a free or inexpensive server and have him support servlets, which can significantly reduce costs.
Flexibility: Because Servlets is running on the Java platform, Servlets can easily be moved from one platform to another, resulting in a much greater flexibility because of the cross-platform nature of Java.
A servlets is actually a Java class that needs to run on a Java Virtual machine and use the Servlets engine. When a servlets is requested, the Servlets engine invokes the Servlets and runs all the time to the invoked Servlets or the Servlets engine is shut down.
The Javaservlets development tool (JSDK) can be downloaded from Sun's web site. He contains Servlets APIs and a simple servlets engine.
What is JSP?
JSP is an extension of servlets technology. Any thing that JSP can do, servlets can be done. But JSP allows you to easily mix Java code with HTML language and perform powerful functions. You can easily read the code and browse to the results of the program execution in the browser.
Here is an example
JSP file:
Hello world! Your name is:<% out.println (response.getparameter ("name"));%>
Servlets file:
Import java.io.*;
Import javax.servlet.*;
Import javax.servlet.http.*;
public class HelloWorld extends HttpServlet
{
public void doget (HttpServletRequest request, httpservletresponse response)
Throws IOException, Servletexception
{
Response.setcontenttype ("text/html");
PrintWriter out = Response.getwriter ();
Out.println ("");
Out.println ("");
Out.println ("");
Out.println ("");
Out.println ("");
Out.println ("Hello world! Your name is: "+ response.getparameter (" name "));
Out.println ("");
Out.println ("");
}
}
The two programs above the output is exactly the same, so you can see that JSP can achieve the general function of Servlets, which JSP program is easier to read and write. JSP and Servlets have different characteristics, the application of different occasions, programmers in use, can be based on their own needs to choose.
OK! How do I get a servlets and JSP running environment?
No matter what server you are using now, you can find the Servlets engine that works for that server.
You can try using these different servlets engines, but I suggest you use Allaire ' s JRun. JRun intercepts requests to servlets and JSPs, and runs the results back to the client via a Web server. JRun is longer than Sun's servlets and supports more features.
JRun Support:
Enterprise Java Beans 1.1
Java Transaction API 1.0
Java Messaging Service 1.0
Java Server Pages The installation of the 1.1
Java servlets 2.2
JRun is simple and easy to set up. and has the ability to set JDBC data sources, so you avoid having to write a lot of code in your program to handle the database problem.
Here's a quick introduction to how to install JRun. Some of the following steps are different from the other engines:
1, make sure your machine has a JDK1.2.2 or higher version installed. Then, close your Web server and close other applications.
2, running the JRun installer.
3, during the installation process, ask you the port number, this port number is used to connect with the server. It is recommended that you use 8000, however you can choose between 8100 and 8199 of the other ports.
4, after installing JRun, set up your Web server to work with JRun. If you are using IIS4.0 or 5.0, open the control plate, select WWW Service, and click the Edit button. Select the Local path field to set execution permissions. You can set this global property, but this can cause security problems. Click OK.
5, now you can run JMC (JRun Management Console) and enter//localhost:8000/in the browser. After logging in as an administrator, you can set up each server. It is recommended that you set the default server first. Of course you can also set up other Web servers, then different servers, the settings may be somewhat different.
OK, now you can test if your jrun is ready to run.
Install your servlets and JSP pages
Compiling your servlets requires you to set up Calsspath. For example, if you use JRun and JDK1.3 in a Windows environment, you can open a command line and then execute the following command:
C:\set Calsspath=c:\ \lib\ext\servlet.jar;%calsspath%.
Copy your servlets to C:\. \servers\default\default-app\web-inf\classes\ under.
Compile your servlets:
C:\...\classess\>javac Helloworld.java
OK, test your servlets, enter in the browser: Http://localhost:8100/default-app/servlet/HelloWorld
In order to run your JSP program, you can just copy the. jsp file to the Default-app directory.
If you are using another server or Servlets engine, the installation steps may be different. But no matter what server or engine you use, you must set up classpath and compile your servlets program.
OK, let's take a look at an example to understand Servlets and JSP
Import java.io.*;
Import javax.servlet.*;
Import javax.servlet.http.*;
public class Helloweb extends HttpServlet
{
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException
{
Response.setcontenttype ("text/html");
PrintWriter out = Response.getwriter ();
Out.println ("
+ "Hello web!");
Out.close ();
}
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException
{
Doget (request, response);
}
}
First, we import java.io.*,javax.servlet.* and javax.servlet.http.*, which contain the classes used in Servlets. In general, you need to include these files in your servlets. Then, we declare our own classes, and our own classes are extended primarily on the basis of classes to complete the functions of our programs. We also need to cover the HttpServlet Doget method. In the main program block above, we must set the content type. You need to set all header information before you use PrintWriter or Servletoutputstream to write to your document. In the program, we use the PrintWriter object to output the information and close the object after use.
We have rewritten the Dopost method. In the above program, this change may not be able to show his effect, however, in many environments you have to do this, for example, if you want to use the Post method to get data instead of getting method, this allows your servlets easy to handle both situations.
Like all technologies, Servlets and JSPs have their