The database used to learn servlet this time is mysql, and I am currently preparing to learn JAVAWeb, so the article about javaweb will definitely continue to be updated later. I actually wanted to learn php a few days ago, but a friend's reminder later made me turn to javaweb.
Because I am currently working on mobile applications, I still plan to start with the data interface when learning Java. The process of explanation will be very careful this time. . . From the way I built it -- the errors I encountered in the middle ------->The last data display will be posted one by one.
1. Install the development environment of javaweb under ecplise:
This step is the same as the previous article javaservlet+oracle, so I directly post the address here
//www.VeVB.COM/article/77065.htm
2. Download the web server software Apatch-Tomcat 7.0:
Download the JAVA WEB server support software Apatch-Tomcat, and then decompress it to any path.
Actually, I suggest that you can create a folder about Java Servlet to store files about it, because there are files like .jar packages that need to be placed. Below is my folder
3. Create a servlet web server environment/create a servlet web project:
1: Add a web server environment
ecplise--->menu bar (windows--->preferences--->Server---->Runtime Environment-->add
2: Create a dynamic web website and add servlet pages
Create a web dynamic website file------>new---->Dynamic Web Project and then directly next. After jumping, you can just take a name and finish creating it.
Create a servlet page: Right-click to select src---->new--->servlet create page in the project
Here is a screenshot after creation:
In the screenshot above
One of the Helper packages is the auxiliary class of mysql, and the second is the auxiliary class of json,
The servlet page is placed in the ServletTest package.
The dbinfo.properties file in the Src folder is my own database connection auxiliary file. You can create this file on the desktop and then copy it to the Src folder. Remember that the path must not be wrong.
Otherwise, an error will be reported if a database connection is used. Below is a screenshot of the content of this file, which is all the information required for mysql database connection.
,
At this point, our javaservlet project and running environment have been created.
4. Add the required .jar package to the Apatch-Tomcat 7.0 lib folder:
Here I only use two jar packages, one is the driver package of mysql and the second is the auxiliary package of json. For details, I'll take a picture
5. Post code screenshots and run screenshots:
1: There are three methods in the servlet page. The first is that the class is constructed and not generally used. The second and third are the methods of processing data requests. The first is get and the other is post, which restricts the way of accessing data. Compared with get, post is more secure, but they are the same for the implementing code for those who write interfaces. . .
Here we use two classes encapsulated in Helpers, MySqlHelper and ResultToJsonTool. In fact, MySqlHelper also encapsulates the method of accessing stored procedures, and the stored procedure access method with out return parameters. I have written an interface before, but the .net technology used is really not so efficient for writing interfaces without these two things. With it, maybe the tasks you complete with 10 SQL statements, only one stored procedure is needed.
2: Output effect
I plan to learn Java well. Most of the time I study in my spare time will use it to read Java. Therefore, I will continue to add articles about Java, and then I will also involve building a Java web operation environment on Linux.