Java Web Development servlet, JSP basics

Source: Internet
Author: User

For many years do not engage in Java Web Development, these days is the national Day holiday, relaxation is also interested in looking back to Java Web Development Technology Foundation.

As we all know, servlet is an important foundation of Java Web development, but because of the relatively cumbersome servlet development, the code is large and difficult to maintain, art can not participate in the interface design and development, and so on, so the birth of JSP. JSP is an important upgrade to the servlet development model. With the Jsp,java Web development technology is really widely used.

First, the Servlet

In Java Web Development, a new class inherits (derives) from the HttpServlet class to create a servlet.

Like what:

@WebServlet (name= "Firstservlet", urlpatterns={"/firstservlet"}) public class Firstservlet extends HttpServlet {/** * Constructor of the object. */public Firstservlet () {super ();} /** * Destruction of the servlet. <br> */public void Destroy () {Super.destroy ();//Just puts "destroy" string in log//Put your code here}/** * the D Oget method of the servlet. <br> * This method was called when a form have its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the Client * @throws servletexception If an error occurred * @throws IOException If an error occurred */public void Doget (Http ServletRequest request, HttpServletResponse response) throws Servletexception, IOException {response.setcontenttype (" Text/html "); PrintWriter out = Response.getwriter (); Out.println ("<! DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" > ") out.println (" <HTML> "); Out.println (" < Head&gT;<title>a servlet</title>This code is generated automatically when you create a servlet in the MyEclipse tool, and you can see that our newly created Firstservlet class inherits from HttpServlet. It also implements the Doget (the method that is called when a GET request), DoPost (the method that is called when the POST request), Init (Initialization object), destroy (destroying the object). It is important to note that we have modified the annotation approach, namely: @WebServlet (name= "Firstservlet", urlpatterns={"/firstservlet"}). This is recommended by servlet3.0, which was previously done by adding servlets and servlet-mapping configurations to Web. Xml. Like what:

<servlet><servlet-name>firstServlet</servlet-name><servlet-class> package name. firstservlet</servlet-class></servlet><servlet-mapping><servlet-name>firstservlet</ Servlet-name><url-pattern>/firstservlet</url-pattern></servlet-mapping>

The above configuration has several points to explain:

1. The Servlet-class node in the servlet needs to fill in the full name of the Servlet class (package name. Class name)

2. The name of the Servlet-name node under servlet-mapping is identical to the name of the Servlet-name node under the servlet to be found.

3, Url-pattern under the URL to add "/"

Start Tomcat and enter the LOCALHOST:8080/project name/firstservet in IE to browse the page (GET request). Port 8080 is the port number of the Tomcat default setting and you can modify the port number in the Server.xml file below tomcat/conf/. For example, if we modify the default port number to 8088 and restart Tomcat (load XML), the URL that is accessed is: localhost:8088/the Urlpattern name of the project name/servlet mapping.

<connector port= "8088" protocol= "http/1.1" connectiontimeout= "20000" redirectport= "8443"/>
The last point to note is that if we modify the code in the servlet, such as re-exporting an HTML table, to recompile, deploy to the Tomcat Web container to run, not as if the JSP was compiled on the fly.

You can see that the servlet is a line of output (the Out.println method), so it's not possible to develop a Java Web project with a pure servlet, but to use it as MVC's C (Controller).

Second, JSP

It is because the pure servlet development Java Web application is very cumbersome, and art and programmers do not work very well, JSP came into being. JSP simplifies development by embedding code in the page, tag libraries (such as the Jstl,java Standard tag library) and using Java code directly in the page. But the nature of the JSP is the servlet, we can use a text editor (such as: EditPlus, notepad++) to open the Tomcat/work directory in the project directory inside the classes file can be seen, In fact, the JSP was compiled to generate a servlet class file (a JSP corresponding to generate a servlet). There are also init, destroy, and service (this is different from the previous get, Post, Put, delete request) and so on.

Here's a quick example of the ability to read data from a MySQL database displayed on the page using JDBC, allowing the reader to quickly get started with JSP.

Development environment:

1, MySQL5.7

2, MyEclipse 2014, JDK7.0

The MySQL installation comes with a MySQL workbench tool that you can use to create a new database.


Click OK and the following screen appears.


Enter your password to log in to the main console interface.


There is a default world database, which can be used to tables (table), Views (view), Stored procedures (stored procedures), Functions (functions) and other related operations.

Of course you can also use the command line on the MySQL console to manipulate the database.


We use the MySQL inside the World database, query the city of the table, the data display (because of the large amount of data, only the first 10).

First in the MySQL tool with SQL query.


With the SQL statement test passed, we can use it directly in the JSP page.

MYJSP.JSP Complete code:

<%@ page language= "java" import= "java.util.*" pageencoding= "utf-8"%><%@ page import= "java.sql.*"%><% String path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

As you can see, the Java code is embedded directly in the JSP page. Since we are using the MySQL database of the JDBC query, we need to mysql-connector-java-5.1.9.jar the jar package as a driver and put it directly into the Web-inf/lib directory of the Web project.

The results shown in the browser are as follows:


As we wish, the first 10 data are shown.

In fact, compare the above page code, you will find this page embed code is very familiar with the way. In ASP, ASP. NET development can be used in the way of embedded code to develop, so basically the development of ideas can be applied to each other.

For space reasons, this blog is first here, the next blog focus on the development of JSP details.


Java Web Development servlet, JSP basics

Related Article

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.