JSP code MVC programming example

Source: Internet
Author: User

I have never understood the role of JAVABEAN in MVC. Now let's look at an instance.

Define a JAVABEAN
Package com. bean;
Public class User {
Private String userName;
Private String userPwd;
Public String getUserName (){
Return userName;
}

Public void setUserName (String userName ){
This. userName = userName;
}

Public String getUserPwd (){
Return userPwd;
}

Public void setUserPwd (String userPwd ){
This. userPwd = userPwd;
}
}

Function of JAVABEAN: it is a PO object that I thought was a VO object. Wrong!

In this way, we can understand that the function of JAVABEAN is actually a thing used to transmit data. In this PHPCMS project, it is equivalent to transmitting a result set. How does the Controller transmit data from the M layer to the view layer for rendering. You can use this method.

Let's look at our Action class.
Package com. dao;
Import java. SQL .*;
Import com. bean. User;
Public class DAO {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
PreparedStatement pstmt = null
Public DAO (){
Try {
Class. forName ("com. mysql. jdbc. Driver ");
Conn = getConnection ();
} Catch (ClassNotFoundException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}

Public Connection getConnection (){
String url = "jdbc: mysql: // localhost: 3306/person ";
String user = "root ";
String password = "123456 ";
Try {
Conn = DriverManager. getConnection (url, user, password );
} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Return conn;
}

Public int addPerson (User user ){
Int flag = 0;
String SQL = "insert into user (user_name, user_pwd) values (?,?) ";
Try {
Pstmt = conn. prepareStatement (SQL );
Pstmt. setString (1, user. getUserName ());
Pstmt. setString (2, user. getUserPwd ());
Flag = pstmt.exe cuteUpdate ();

} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Return flag;
}

Public ResultSet showPerson (){
String SQL = "select * from user ";
Try {
Pstmt = conn. prepareStatement (SQL );
Rs = pstmt.exe cuteQuery ();
} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Return rs;
}

Public void close (){
Try {
If (rs! = Null) rs. close ();
If (pstmt! = Null) pstmt. close ();
If (conn! = Null) conn. close ();

} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}

This class also places database connection operations here. It is actually an incorrect method. The correct method should be to write the database layer to a CLASS separately. But here let's take a look:

Public int addPerson (User user ){
Int flag = 0;
String SQL = "insert into user (user_name, user_pwd) values (?,?) ";
Try {
Pstmt = conn. prepareStatement (SQL );
Pstmt. setString (1, user. getUserName ());
Pstmt. setString (2, user. getUserPwd ());
Flag = pstmt.exe cuteUpdate ();
} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Return flag;
}

This DAO object is used to receive data from the JAVABEAN layer.

Let's see how to SET the value to this BEAN.

Package com. servlet;
Import java. io. IOException;
Import java. io. PrintWriter;
Import javax. servlet. ServletException;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Import com. bean. User;
Import com. dao. DAO;
Public class AddUser extends HttpServlet {
Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
String userName = request. getParameter ("userName ");
String userPwd = request. getParameter ("userPwd ");
User user = new User ();
User. setUserName (userName); // It depends on the SET and GET methods !!!!
User. setUserPwd (userPwd );
DAO dao = new DAO ();

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.