Install your Servlet and JSP page
Compile your Servlets and you need to set CALSSPATH. For example, if you use JRun and JDK1.3 in windows, you can open a command line and execute the following command:
C: \ set CALSSPATH = C :\\ lib \ ext \ servlet. jar; % CALSSPATH %
Copy your Servlets to C: \ servers \ default-app \ WEB-INF \ classes. Compile your Servlets:
C: \... \ classess \> javac HelloWorld. java
Now, test your Servlets and enter http: // localhost: 8100/default-app/servlet/HelloWorld in your browser. To run your JSP program, you just need. copy the jsp file to the default-app directory.
If you are using another server or Servlets engine, the installation steps may be different. However, no matter what server or engine you use, you must set CLASSPATH and compile your Servlets program.
Now let's look at an example to understand Servlet 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, import java. io. *, javax. servlet. *, and javax. servlet. http. *. These files contain classes used in Servlets. In general, you need to include these files in your Servlet and JSP. Then, we declare our own classes. Our own classes are mainly extended based on the basic classes to complete the functions in our programs. We also need to overwrite the doGet method of HttpServlet. In the above main program block, we must set the content type. Before using PrintWriter or ServletOutputStream to write to a document, you must set all header information. In the program, we use the PrintWriter object to output information and close this object after use.
We have rewritten the doPost method. In the above program, this modification may not reflect its effect. However, in many environments, you also need to do so, for example, if you want to use the POST method to obtain data instead of the GET method, this allows your Servlets to easily process the two cases.
Like all technologies, Servlet and JSP have their
- Servlet Container session tracking
- Detailed description of Servlet instances
- Introduction to Servlet read request headers
- Call the Http Servlet Request Method
- Basic Structure of Servlet