Servlet JSP configuration is the basis for learning. How can we build a good running platform for Servlet JSP? First, download the tool:
I suggest using Editplus + JDK for beginners. I think it is easier to use Editplus + JDK for beginners, such as JB, Eclipse, and JCreator, but it does not know how to configure environment variables for beginners, in this way, it is difficult to reach the point of knowing and knowing why.
You can download it at the following address:
Editplus (the latest version is v2.11): http://count.skycn.com/softdown.php? Id = 3641 & url = http://sc-http.skycn.net/down/epp211a_cn.exe JDK (the latest version is Java2sdk1_4_2): http://count.skycn.com/softdown.php? Id = 3116 & url = http://sc-http.skycn.net/down/j2sdk-4104_2-windows-i586.exethis is for Windows)
Then install JDK. I installed it under the c: \ JDK directory.
Then the CLASSPATH is set:
Just as the operating system uses PATH to search for executable programs, the Java Runtime Environment will traverse CLASSPATH to find classes. Even a simple program like HelloWorld, JVM will traverse every PATH defined by CLASSPATH, until the corresponding file is found.
I believe that if your system is not 2 K or XP, you should set the Path as follows:
My computer-> properties-> advanced-> Environment Variables
Append the following Path to the environment variable: C: \ JDK \ bin;.; C: \ JDK \ lib.
You can also configure C: \ JDK \ bin;.; C: \ JDK \ lib \ dt. jar; C: \ JDK \ lib \ tools. jar.
◆ Remember: In the environment variables, please remember not to be less. It indicates the current path. If there are fewer errors, you will say it!
Dt. jar is a class library about the runtime environment, and tools. jar is a class library about some tools.
If C: \ JDK \ bin is not configured in the Servlet JSP configuration, "javac" is not an internal or external command, or a program or batch file that can be run ." This error occurs.
Let's write an example program:
Open Editplus and create a new Java file. Follow the input below to keep it case sensitive:
- public class HelloWorld{
- public static void main(String[] args){
- System.out.println("Hello,World!");
- }
- }
Then save the file (ctrl s) to HelloWorld. java. Java is case sensitive, so the case must be distinguished, that is, HelloWorld. java is not helloworld. java or other.
Run: Start-> Run-> cmd
Switch the directory to the current directory in the console:
- javac HelloWorld.java
- java HelloWorld
You will see the output Hello, World on the console!
Javac is a compilation command that compiles HelloWorld. java into HelloWorld. class.
Java is the command to explain. JVM will explain and execute HelloWorld. class.
At this time:
1. If Exception in thread "main" java. lang. NoClassDefFoundError: HelloWorld
That is, you didn't add that. (dot) to the environment variable)
2. If Exception in thread "main" java. lang. NoSuchMethodError: main
Or HelloWorld. java: 1: Public class helloworld must be defined in a file called
"HelloWorld. java ".
That is, you did not write this HelloWorld case-insensitive, or you did not save it as HelloWorld. java. This name must be the same as the public class name.
Let's talk about the problem of environment variables. Next, let's talk about how to compile and run the environment variables in Editplus, and choose Tools> parameter settings> Configure user Tools.
1. Add tools to add applications)
Menu text: Compile Java Program
Program: C: \ JDK \ bin \ javac.exe
Parameter: File Name
Initial Directory: file directory
2. Add tools to add applications)
Menu text: Run Java Program
Program: C: \ JDK \ bin \ java.exe
Parameter: the file name does not contain the extension)
Initial Directory: file directory
You can add a tool group name as needed, such as Debug Java Program.
Then, in the drop-down menu of Tools, you will see the Compile Java Program and Run Java Program options. Then you can use ctrl 1 to Compile and ctrl + 2 to Run the Program.
Next we will discuss the runtime environment after Servlet/JSP Configuration:
To run Servlet, JSP/Servlet container is required. We recommend that you use Tomcat for beginners.
Tomcat Latest Version 5.0): http://cvs.apache.org/builds/jakarta-tomcat-5/nightly/jakarta-tomcat-5-bin-20030725.zip
Decompress the package:
C: \ Tomcat
Then configure the environment variables. Add three system variables:
- JAVA_HOME: C:\JDK
- TOMCAT_HOME: C:\Tomcat
- CLASSPATH: %JAVA_HOME%\lib;%TOMCAT_HOME%\lib
The environment variables of Tomcat are configured. Check whether Tomcat can run as follows:
In the console, go to the C: \ Tomcat \ bin directory and run startup. Then, a window is displayed, which means that the Server is running.
Enter http: // localhost: 8080 in the browser. If the welcome page appears, it means Tomcat is okay. Write your first Servlet as above.
- 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
- {
-
- response.setContentType("text/html");
- PrintWriter out = response.getWriter();
- out.println("﹤html﹥﹤head﹥﹤title﹥");
- out.println("This is my first Servlet");
- out.println("﹤/title﹥﹤/head﹥﹤body﹥");
- out.println("﹤h1﹥Hello,World!﹤/h1﹥");
- out.println("﹤/body﹥﹤/html﹥");
-
- }
- }
Then use javac HelloWorld. java to compile the file. If the file cannot be imported javax. servlet .*
Then, copy the servlet. jar file in C: \ Tomcat \ common \ lib to C: \ JDK \ jre \ lib \ ext and compile it again!
Then, in the Tomcat directory C: \ Tomcat \ webapps \ ROOT, follow the following file structure:
- ROOT \ index.html
- ROOT \ welcom. jsp
- ROOT \ WEB-INF \ lib \ MyServlet. jar if your servlet. class is typed into a. jar file, put it under lib)
- ROOT \ WEB-INF \ classes \ HelloWorld. class (put the above generated HelloWorld. class file in this)
Then, enter http: // localhost: 8080/servlet/HelloWorld in the browser. The Error 404 -- Not Found is returned by the Server.
What's going on?
ServletJSP configuration Servlet must use C: \ Tomcat \ webapps \ ROOT \ WEB-INF directory under the web. xml file registration, open this web. xml file with EP, add in it:
- ﹤servlet﹥
- ﹤servlet-name﹥HelloWorld﹤/servlet-name﹥
- ﹤servlet-class﹥HelloWorld﹤/servlet-class﹥
- ﹤/servlet﹥
- ﹤servlet-mapping﹥
- ﹤servlet-name﹥HelloWorld﹤/servlet-name﹥
- ﹤url-pattern﹥/servlet/helloworld﹤/url-pattern﹥
- ﹤/servlet-mapping﹥
Such a structure
- ﹤servlet﹥
- ﹤servlet-name﹥HelloWorld﹤/servlet-name﹥
- ﹤servlet-class﹥HelloWorld﹤/servlet-class﹥
- ﹤/servlet﹥
Indicates the included servlet class. The following structure:
- ﹤servlet-mapping﹥
- ﹤servlet-name﹥HelloWorld﹤/servlet-name﹥
- ﹤url-pattern﹥/servlet/HelloWorld﹤/url-pattern﹥
- ﹤/servlet-mapping﹥
Specifies the URL mode to which HelloServlet maps.
Servlet JSP configuration should be gradually consolidated for beginners. Don't be flustered when errors occur. Check whether each step is correct. After Servlet JSP is configured, let's start programming!
- Automatic jump to error page for JSP Servlet instance
- 8-point discussion on optimizing JSP Servlet applications
- Select JSP development tools
- Servlet and JSP paths
- Analysis of form data in JSP Servlet Technology