Basic MVC instance analysis for Java Web implementation, and mvc instance analysis
This article describes the basic MVC of Java Web implementation. Share it with you for your reference. The details are as follows:
Login. jsp -- input file in view
Success. jsp -- View output file
Failure. jsp -- View output file
LoginBean. java -- Model Section
LoginServlet. java -- controller part
Web. xml -- web application configuration file
The following sections describe:
1. login. jsp
The input file of this function. The user first accesses this file. It is mainly used to enter the user name and password.
The Code is as follows:
<% @ Page contentType = "text/html; charset = gb2312" %> <script language = "JavaScript"> function isValidate (form) {// obtain username = form. username. value; userpass = form. userpass. value; // determine the username length if (! MinLength (username, 6) {alert ("the username length is less than 6 characters! "); Form. username. focus (); return false;} if (! MaxLength (username, 8) {alert ("the username length is greater than 8 characters! "); Form. username. focus (); return false ;}// determine the password length if (! MinLength (userpass, 6) {alert ("the password length is less than 6 characters! "); Form. userpass. focus (); return false;} if (! MaxLength (userpass, 8) {alert ("the password length is greater than 8 characters! "); Form. userpass. focus (); return false;} return true;} // verify whether the minimum length function minLength (str, length) {if (str. length> = length) return true; else return false;} // determines whether the maximum length of the function maxLength (str, length) {if (str. length <= length) return true; else return false ;} </script>
The Code provides the client verification function (the length of the user name and password is 6-8 characters ). After the verification is passed, the request is submitted to the Controller Servlet.
2. success. jsp
After successful logon, you will be redirected to this interface. The Code is as follows:
<% @ Page contentType = "text/html; charset = gb2312 "%>
Use Expression Language in the code to display the user information after logon on the street.
3. failure. jsp
After logon fails, the page is displayed. The Code is as follows:
<% @ Page contentType = "text/html; charset = gb2312 "%>
The Code provides a hyperlink to link to the logon interface.
4. LoginBean. java
The logon function is completed. If the user name and password are the same, the logon is successful.
package beans;public class LoginBean { public boolean validate(String username,String userpass){ return username.equals(userpass); }}
5. LoginServlet. java
The file is controlled. The main functions are described as follows:
①. Obtain the user name and password entered by the user from login. jsp;
②. Create the LoginBean object and call the LoginBean method validate;
③ Select success. jsp or failure. jsp to respond to the user based on the result returned by the method.
The complete code is as follows:
Package servlets; import java. io. IOException; import java. io. printWriter; import javax. servlet. *; import javax. servlet. http. *; import beans. *; public class LoginServlet extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost (request, response);} public void doPost (HttpServletRequest request, httpServletResponse response) throws ServletException, IOException {// obtain the user ID and password String username = request. getParameter ("username"); String userpass = request. getParameter ("userpass"); // create the model object LoginBean loginBean = new LoginBean (); // call the business method to verify boolean B = loginBean. validate (username, userpass); // String forward of the file to be switched; // If the login succeeds, write the username to the session and switch to success. jsp, // otherwise switch to failure. jsp if (B) {// obtain session HttpSession session = (HttpSession) request. getSession (true); // Save the user name to the session. setAttribute ("username", username); // The target conversion file is success. jsp forward = "success. jsp ";} else {// The target redirection file is failure. jsp forward = "failure. jsp ";} // get the Dispatcher object RequestDispatcher dispatcher = request. getRequestDispatcher (forward); // jump to dispatcher. forward (request, response );}}
In the code, the user information of the logged-on user is stored in the session, which is also handled in actual applications.
6. web. xml
The main code is Servlet configuration. The Code is as follows:
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>LoginServlet</servlet-name> <servlet-class>servlets.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>login</url-pattern> </servlet-mapping></web-app>
I hope this article will help you with JSP program design.