Demo of MVC mode in JSP

Source: Internet
Author: User

 

What is the MVC pattern... Let's talk about its process.

 

A form is defined in login. jsp: <form action = "Controller" name = "loginForm" method = "post">

 

This completes the entry from layer V to layer C.

 

 

Controller. java is a Servlet. to inherit from HttpServlet, you must first arrange it in the configuration file. The code is:

 

String id = request. getParameter ("userId ");

String name = request. getParameter ("userName ");

/// Use the model (UserBeanDL) to process the user

/// 1. Create a UserBean processing object

UserBeanDL ubd = new UserBeanDL ();

Try {

Int iId = Integer. parseInt (id );

If (ubd. checkUser (iId, name )){

System. out. println ("control via Servlet ");

/// When you jump to the wel. jsp page, prepare the data to be displayed.

ArrayList al = ubd. getUsersByPage (1 );

Int pageCount = ubd. getPageCount ();

//// Pass the parameter. Here, an ArrayList is passed.

 

Request. setAttribute ("result", al );

 

//// Note: The following parameters are objects. You need to set int --> Object (String)

 

Request. setAttribute ("pageCount", pageCount + "");

 

/// Turn

// Response. sendRedirect ("welcom. jsp ");

/// Because the sendRedirect method is inefficient, it is a common forwarding method in software companies.

/// This method is efficient, and objects in the request can also be used on the next page.

Request. getRequestDispatcher ("threedown/welcome. jsp"). forward (request, response );

} Else {

Request. getRequestDispatcher ("three/login. jsp"). forward (request, response );

}

......

 

 

In UserBean., all operations on the database are as follows:

 

Public class UserBeanDL {

Connection conn = null;

PreparedStatement ps = null;

ResultSet rs = null;

Private int pageCount = 0;

Int pageSize = 3;

Int rowCount = 0;

//// Close the resource

//// Obtain the total number of rows

//// Get pageCount

/// // Add (over)

//// Modify (over)

//// Delete

/// // Search

//// Verify whether the user exists

 

UserBean. java is an entity class.

 

 

In Welcome. jsp:

 

<Tr> <td> ID </td> <td> name </td> <td> age </td> </tr>

<%

Int pageNow = 1;

String szPageNow = request. getParameter ("pageNow ");

If (szPageNow! = Null)

{

PageNow = Integer. parseInt (szPageNow );

}

ArrayList al = (ArrayList) request. getAttribute ("result ");

For (int I = 0; I <al. size (); I ++)

{

UserBean ub = (UserBean) al. get (I );

%>

<Tr> <td> <% = ub. getId () %> </td>

<Td> <% = ub. getName () %> </td>

<Td> <% = ub. getAge () %> </td> </tr>

<%

}

/// From object --> string -- int

Int pageCount = Integer. parseInt (String) request. getAttribute ("pageCount "));

// Int pageCount = ubd. getPageCount ();

For (int I = 1; I <pageCount; I ++ ){

%>

<A href = UserDL? PageNow = <% = I % >><%= I % >></a>

<%

} Www.2cto.com

%>

</Table>

 

UserDL. java is a Servlet dedicated for paging

 

Public void doPost (HttpServletRequest request, HttpServletResponse response)

Throws ServletException, IOException {

System. out. println ("Paging through Servlet ");

String s_pageNow = request. getParameter ("pageNow ");

UserBeanDL ubd = new UserBeanDL ();

Try {

Int pageNow = Integer. parseInt (s_pageNow );

ArrayList al = ubd. getUsersByPage (pageNow );

Int pageCount = ubd. getPageCount ();

Request. setAttribute ("pageNow", pageNow );

Request. setAttribute ("result", al );

Request. setAttribute ("pageCount", pageCount + "");

Request. getRequestDispatcher ("threedown/welcome. jsp"). forward (request, response );

} Catch (Exception e ){

E. printStackTrace ();

}

}

 

Excerpted from the column of wandering Xiaoqi

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.