In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how Servlet runs". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
1. Servlet tutorial Servlet infrastructure
Let's talk about the simplest servlet framework for handling user GET requests. A GET request is a request made when a user enters an address in the browser's address bar, clicks on a link on a web page, or generates an HTML form with no defined method. Servlets can also easily handle form submission (POST), which we'll talk about in the next few sections.
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class SomeServlet extends HttpServlet { public void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //Use "request" to read http headers (e.g. cookies) //and HTML form data (such as user input and submitted data) //specify http response and http header with "response" // (e.g. specify type of message, set cookie). PrintWriter out = response.getWriter(); //output content to browser with "out" } }
Whether you use the doGet or doPost methods, servlets use the HttpServlet extension class. These methods fall into two categories: HttpServletRequest and HttpServletResponse. HttpServletRequest contains methods for obtaining information such as form data, HTTP messages, etc. HttpServletResponse contains the HTTP response (200, 404, etc.), header (Content-Type, Set-Cookie, etc.) More importantly, you can output information to clients using PrintWriter methods. Note that the doGet and doPost methods throw two exceptions, so you must include them in your definition. To use PrintWriter, HttpServlet, and HttpServletRequest, the HttpServletResponse method must also include java.io, javax.servlet, and javax.servlet.http, respectively. Generally, doGet and doPost are called by the service method, but sometimes you might want to bypass the service method and use your own service method, such as defining a servlet that can handle both GET and POST requests.
2. Servlet tutorial one of the simple Servlet--generate save text
The following is an example of a simple servlet that generates stored text:
HelloWorld.java import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println("Hello World"); } }
◆ Compile and install Servlets
Note that the method of installing servlets varies from web server to web server. Please refer to the installation documentation that comes with your web server for details. Java Web Server(JWS)2.0 comes with an online instance. In JWS, servlet programs must be placed in the servlets directory of the installation directory to run. If your web server is used by multiple people and you don't have a good virtual server infrastructure to automatically avoid conflicts, you can also create a separate package (I call it hall) under the servlets directory as I did--create a hall subdirectory under the servlets directory and put your servlets in that directory. Here I put HelloWorld.java in the hall directory. Most of the other server installations are similar, and the servlet and JSP examples in this tutorial were tested in BEA Weblogic and IBM WebSphere 3.0. WebSphere has an excellent mechanism for virtual servers, so there is no need to use packages separately for name conflicts.
If you've never used a package before, there are two ways to compile class files within a package.
One way to do this is to set up CLASSPATH to point to the directory that contains your servlet file so that it compiles properly in that directory. For example, if your servlet directory under Windows is C: Java Web Server servlets and the package name (i.e., subdirectory) is hall, then the dos window settings are as follows:
DOS> set CCLASSPATH=C:JavaWebServerservlets;%CLASSPATH% DOS> cd C:JavaWebServerservletshall DOS> javac YourServlet.java
*** Step, set path. *** One step at a time, so as not to have to reopen a dos window each time. Add "set CLASSPATH=..." to autoexec.bat under Windows 95/98 "expression, pointing CLASSPATH to servlet.jar and jsp.jar. Under NT, start Menu-Settings-Control Panel, select "System," select "Environment," and enter variable names and variable values. Note that if your package name is in the format name1.name2.name3, you should also set CLASSPATH to point to the top level of the package (i.e. name1).
The second method is to go to the upper directory of the package and execute "javac directoryYourServlet.java" (note the reverse slant under Windows) or "javac directory/YourServlet.java" under Unix (note the positive slant under Unix). For example, if your servlet directory under windows is C:JavaWebServerservlets, and the package name is the subdirectory name hall, then perform the following operations:
DOS> cd C:JavaWebServerservlets DOS> javac hallYourServlet.java
Note that most JDK 1.1 versions of javac for Windows require an anti-skew bar, which was later fixed in JDK 1.2, but since many web servers are configured according to JDK 1.1, many servlet authors insist on an anti-skew bar for convenience.
The javac-d option can be used to specify that.class files and source files are placed in separate places.
◆Servlet tutorial Running Servlet
Servlets are typically placed in the servlets directory under the JWS installation directory and called via http://host/servlet/ServletName. Note that Servlets in the Servlets directory here have an "s" but not in the URL address bar.
The above example is in the hall package, so call it like this: http://host/servlet/hall.HelloWorld. Other servers may differ slightly in Servlet placement directories and calls. Most servers allow you to define mappings to servlet directories, so a servlet can also be called from http://host/any-path/any-file.html. Depending on the server, you can check the documentation that comes with the server for details.
"Servlet how to run" content introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 240
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.