Servlet/JSP Configuration

Source: Internet
Author: User

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:

 
 
  1. public class HelloWorld{  
  2. public static void main(String[] args){  
  3. System.out.println("Hello,World!");  
  4. }  

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:

 
 
  1. javac HelloWorld.java  
  2. 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:

 
 
  1. JAVA_HOME: C:\JDK  
  2. TOMCAT_HOME: C:\Tomcat  
  3. 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.

 
 
  1. import java.io.*;  
  2. import javax.servlet.*;  
  3. import javax.servlet.http.*;  
  4. public class HelloWorld extends HttpServlet  
  5. {  
  6. public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException  
  7. {  
  8.  
  9. response.setContentType("text/html");   
  10. PrintWriter out = response.getWriter();  
  11. out.println("﹤html﹥﹤head﹥﹤title﹥");  
  12. out.println("This is my first Servlet");  
  13. out.println("﹤/title﹥﹤/head﹥﹤body﹥");  
  14. out.println("﹤h1﹥Hello,World!﹤/h1﹥");  
  15. out.println("﹤/body﹥﹤/html﹥");  
  16.  
  17. }  
  18. }  

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:

 
 
  1. ROOT \ index.html
  2. ROOT \ welcom. jsp
  3. ROOT \ WEB-INF \ lib \ MyServlet. jar if your servlet. class is typed into a. jar file, put it under lib)
  4. 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:

 
 
  1. ﹤servlet﹥  
  2. ﹤servlet-name﹥HelloWorld﹤/servlet-name﹥  
  3. ﹤servlet-class﹥HelloWorld﹤/servlet-class﹥  
  4. ﹤/servlet﹥  
  5. ﹤servlet-mapping﹥  
  6. ﹤servlet-name﹥HelloWorld﹤/servlet-name﹥  
  7. ﹤url-pattern﹥/servlet/helloworld﹤/url-pattern﹥  
  8. ﹤/servlet-mapping﹥  

Such a structure

 
 
  1. ﹤servlet﹥  
  2. ﹤servlet-name﹥HelloWorld﹤/servlet-name﹥  
  3. ﹤servlet-class﹥HelloWorld﹤/servlet-class﹥  
  4. ﹤/servlet﹥  

Indicates the included servlet class. The following structure:

 
 
  1. ﹤servlet-mapping﹥  
  2. ﹤servlet-name﹥HelloWorld﹤/servlet-name﹥  
  3. ﹤url-pattern﹥/servlet/HelloWorld﹤/url-pattern﹥  
  4. ﹤/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!

  1. Automatic jump to error page for JSP Servlet instance
  2. 8-point discussion on optimizing JSP Servlet applications
  3. Select JSP development tools
  4. Servlet and JSP paths
  5. Analysis of form data in JSP Servlet Technology

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.