Learn JSP: Second step to create a JSP Web Project

Source: Internet
Author: User
Tags tomcat server

Learn JSP above: First step ECLIPSE+TOMCAT+JRE (configuration Environment)

Create New Project (Dynamic Web project)

1. Select "File-->new-->dynamic Web project" to create the Tomcattest project:

Next→next

To get an engineering structure like this, each directory resolves:

    • Deployment Descriptor: Description of the deployment.
    • Lib: The bag you add can be placed inside.
    • Build: Put the compiled file.
    • The Src:java file is placed inside.
    • The webcontent:jsp page is put into the inside.

2. Create a new firstjsp.jsp file under the WebContent folder.

Choose as needed, because in this case we're just trying to print a hello! , so choose the simplest HTML template, you can try all the items:

3, modify the JSP file, in the body tag with <%%>jsp tag, add the following code:

<%    out.println ("Hello JSP!") ); %>

4. Before running the program, modify the browser options:

Use the browser you want to use to test your JSP, in this case, choose Firefox (the browser installed on your PC will appear here)

5. Run the JSP on Web Server (Tomcat).

Right-click on the JSP file →run on Server

Select the Web server you want to use in the Automatically opened page (which will be displayed here if you have more than one server)

If you want to run the JSP page with this Web server in the future, you can check always using this server when the running this project. (recommended)

Observe the startup information in the console.

It indicates the use of the Web Server=apache Tomcat7.0.85, and Jre=c:\eclipse/jre, is the same as we configured above.

In addition, the port number at the arrow is I modified, originally: 8080 and 8009, modify the port number is to avoid multiple Web server port conflicts, the method of modification in my blog has written, Baidu is also easy to check.

The last line is the information for the successful startup server. This is the same effect as executing startup.bat in the bin of Tomcat.

6, eclipse will automatically jump to the specified browser, rendering the following page.

http://localhost:7780/JSPdemo1/FirstJSP.jsp

7, view the page source code, understand the JSP running on the server side.

On the page, right-click →view page Source

Remember how we wrote it here?

Description: Jsp=servlet, Essence =java. The server-side interpretation is rendered by Web server as HTML in the browser page (client).

So in the browser is not to see the source code of JSP.

below, a simple introduction to servlet creation.

1. Create a servlet in the above project. It is recommended that Java files be placed in the package.

Once created, you can see that the servlet is a Java file:

Eclipse will automatically generate code as follows, followed by syntax.

Modify the code in the doget method as follows:

    protected void throws servletexception, IOException {        //  TODO auto-generated method stub        Response.getwriter (). Write ("Hello Servlet!") );    }

Restart the Tomcat server:

2, run. Right-→run on Server (same as JSP file) on Firstservlet.java file

The browser opens automatically to see the results of a successful run:

Http://localhost:7780/JSPdemo1/FirstServlet

Description:firstservlet is the content of this tag in the code @webservlet("/firstservlet"), instead of the class name, experiment to see.

Change @webservlet tag to changeservletname:

To restart Tomcat, run the result:

http://localhost:7780/JSPdemo1/Changeservletname

Learn JSP: Second step to create a JSP Web Project

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.