Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to treat Servlet and Jsp knowledge points

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to treat Servlet and Jsp knowledge points, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

1. First of all, you need some tools:

Tomcat,mysql,Navicat For mysql,eclipse (javaee)

two。 Create database 3. Create a new web project

Note that Dynamic Web Project,module version can choose either 2.5 or 3.0.

4. Deploy tomcat

Create a new server and complete the following configuration when you are done, so that you can use the tomcat server directly in eclipse. If you choose the first one here, eclipse will download a simple version of tomcat himself.

5. Overall grasp

MVC development mode is required to query or add to the database on a web page. Here is a diagram, which is roughly like this:

6. The order of writing code is from bottom to top: pojo layer-Dao layer-Service layer-servlet layer 6.1pojo layer code writing:

Write a class corresponding to the database, with the package name ending with .pojo. The set get method, the construction method, the no-parameter construction method, and the toString method are all written over and over again. Anyway, alt+s is generated automatically.

Package com.pojo;public class User {private int uid; private String uname; private String pwd; private String sex; private int age; private String birth; public int getUid () {return uid;} public void setUid (int uid) {this.uid = uid } public String getUname () {return uname;} public void setUname (String uname) {this.uname = uname;} public String getPwd () {return pwd;} public void setPwd (String pwd) {this.pwd = pwd } public String getSex () {return sex;} public void setSex (String sex) {this.sex = sex;} public int getAge () {return age;} public void setAge (int age) {this.age = age } public String getBirth () {return birth;} public void setBirth (String birth) {this.birth = birth;} public User (int uid, String uname, String pwd, String sex, int age, String birth) {super (); this.uid = uid; this.uname = uname This.pwd = pwd; this.sex = sex; this.age = age; this.birth = birth;} public User () {super () } @ Override public String toString () {return "User [uid=" + uid + ", uname=" + uname + ", pwd=" + pwd + ", sex=" + sex + ", age=" + age + ", birth=" + birth + "]";}} 6.2Dao layer code 6.2.1 interface package com.dao;import java.util.List Import com.pojo.User;public interface UserDao {/ / Log in to User checkUserLoginDao (String uname,String pwd); / / password modification int userChangePwdDao (String newPwd, int uid); / / display all information List showAllDao (); / / insert user int userRegDao (String uname,String pwd, String sex, String age, String birth);} 6.2.2 API instantiated object

Here, take an insert and query method as an example, just follow the basic steps.

Query:

Public User checkUserLoginDao (String uname, String pwd) {/ / create jdbc object Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; / / create entity class object User u = null Try {/ / load driver Class.forName ("com.mysql.jdbc.Driver"); / / get connection conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/test2", "root", "lijingjing") / / create the sql statement String sql = "select * from t_user where uname=?" And pwd=? "; / / create sql object ps = conn.prepareStatement (sql); / / assign ps.setString (1, uname) to placeholder; ps.setString (2, pwd) / / execute sql rs = ps.executeQuery (); / / traverse the result set while (rs.next ()) {u = new User (); u.setAge (rs.getInt ("age")) U.setUid (rs.getInt ("uid")); u.setBirth (rs.getString ("birth")); u.setPwd (rs.getString ("pwd")); u.setSex (rs.getString ("sex") U.setUname (rs.getString ("uname"));}} catch (Exception e) {e.printStackTrace ();} finally {try {rs.close () } catch (SQLException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} try {ps.close () } catch (SQLException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} try {conn.close () } catch (SQLException e) {/ / TODO Auto-generated catch block e.printStackTrace ();}} return u;}

The inserted code flow is more or less the same as the query, except that the rs result set is not required, and the return value is of type int (indicating the number of rows affected), index = ps.executeUpdate ()

In addition, in order to ensure that Chinese can be displayed normally in the database, when loading the driver, add useUnicode=true&characterEncoding=UTF8 to the database.

Conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=UTF8", "root", "lijingjing")

In this way, the data access layer is finished.

6.3Service layer code 6.3.1 interface

Generally, this layer performs business logic processing on the data fetched from the Dao layer, and here it is simply returned.

Package com.service;import java.util.List;import com.pojo.User;public interface UserService {/ / Log in to User checkUserLoginService (String uname,String pwd); / / modify password int userChangePwdService (String newPwd, int uid); / / display all user information List showAllService (); / / register void userRegService (String uname,String pwd, String sex, String age, String birth) } 6.3.2 the instantiated object of the interface

Inherit the above interface, implement its method, create a Dao layer object, return it, the code is not on.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report