Compile Servlet in JB6
Step 1: Establish an engineering
We take a Hello World program as an example. In order to run a servlet displaying Hello World in JB6, first create a project file: steps are as follows: 1. Select menu item "File | New Project" Display Project Wizard. 2. Enter "SimpleEt" in the Name Field. 3. Select "Generate Project Notes File". 4. Click Next to enter the second step. 5. Click "Finish" Turn off the Engineering Wizard dialog and generate an engineering file. You don't have to make any changes to the default option in step 2, 3. (Translator attached: You can change the path of saved files) OK! Engineering file SimpleServlet.jpx and The HTML page file of the project file is now displayed on the engineering panel. In the next step, you will build a webApp for your servlet, even if you don't want to debug your project file (Project Files, but must be established in the real environment A webApp.
Step 2: Create webapp
When debugging a web application, a webApp will generate in your project, which includes the content file of your web application, you want to create a webapp: 1. Select "File | New" to display Object Gallery, click "Web" tab and select "Web Application", click "OK". The web application wizard is displayed. 2. Enter SIMPLETWEBAPP in the Name Field. 3. Enter WebApp in Directory Field. 4. Cancel the selection of the "Generate WAR" check box. 5. Click on "OK" to close the wizard and generate webapp. At this time, WebApp SimpleServletWebApp is displayed in the engineering panel as a node. In the next step, we will establish a sevlet.
Step 3: Establish a servlet with a servlet wander
In this step, we will use the servlet wizard to create a servlet, you will use the wizard as follows: * Enter the class name of the servlet. * Select the type of servlet and its content. * Select the HTTP method and overwrite it. * Create a use To run the SHTML file for servlet * to establish parameters for servlet. Procedure as follows: 1. Select "File | New" to display Object Gallery. 2. Click the "Web" tab and select "Servlet", click "OK" "Servlet Wizard" "3. In this first step, use the default option without having to change. 4. Click" Next "to enter the second step. 5. In the second step, select" DOPOST () "method and ensure" doget () "Method is selected, select" Generate Shtml File "and select" Generate
1. Open servlet1.java in the editing window, select "Search | Find Command" to find the first line with the "/ ** Initialize Global Variables ** /" keyword, in front of the line, enter the following Code: int connections = 0; this line creates a variable named Connections and initials to 0. 2. Searching with one line containing the following strings: The servlet HAS Received A Post. This the reply is added in the next row of the line as follows Code: Out.Println ("
Thanks for Visiting,"); OUT.Println ("UserName"); Out.Println ("
"); Out.println ("Hello World - My First Java Servlet Program! "); Out.Println ("
you are visitor number "); Out.println (Integer.Tostring ( Connections)); these row code acquire username information and use a" OUT. Println statement display. Then the code increases the number of access and display. 3. Select "File | Save" Save. In the next step, you will compile and run the servlet. Step 5: Compile and run the servlet
1. Select "Project | make project'simpletervlet.jpx. '" 2. Right click on the servlet1.shtml file on the engineering panel. 3. Select "Web Run"
Note: You can also click the mouse button on the Java file in the engineering panel and select "Web Run" to compile the servlet, in this example, you are running servlet on the SHTML file, because you have already in the servlet wizard Enter the domain of the parameter and the button for submitting information defined in the SHTML file.
Running the SHTML file will start JBuilder's default server Tomcat, Tomcat's output information will be displayed in the message panel. HTTP command and parameter values will also be output on the panel. Two new labels about servlet Out of the content panel now: Web View and Web View Source. Servlet running in Web View. To run the servlet, you should enter your username in the text box and click the Submit button. DOPOST () method Called, the response result is displayed in "Web View". Click "Back" to return, and enter the username submit to run and observe the value of the discovery counter is increasing. You can click the red square button in the lower left to stop the server. If you change your code slightly, you can close your server before you compile or run. Ok! Our first servlet program is completed.