Basic MVC Example Analysis of Java Web implementation _jsp programming

Source: Internet
Author: User
Tags java web

This example describes the basic MVC of Java Web implementations. Share to everyone for your reference. Specifically as follows:

Input file for login.jsp--view section
Output file for success.jsp--View section
Output file for failure.jsp--View section
loginbean.java--Model Section
loginservlet.java--Controller Section
Web.xml--web application configuration file

The following are described separately:

1, login.jsp

The input file for this feature, the user first accesses this file. Used primarily for entering user names and passwords.
The code is as follows:

<%@ page contenttype= "text/html;charset=gb2312"%> <script language= "JavaScript" > Function isvalidate (
   form) {//Get user input information username = Form.username.value;
   Userpass = Form.userpass.value; Determine user name length if (!minlength (username,6)) {alert ("username length is less than 6 bits!")
     ");
     Form.username.focus ();
   return false; } if (!maxlength (username,8)) {alert ("User name is longer than 8 bits!")
     ");
     Form.username.focus ();
   return false; //Judge Password length if (!minlength (userpass,6)) {alert ("password length is less than 6 bits!")
     ");
     Form.userpass.focus ();
   return false; } if (!maxlength (userpass,8)) {alert ("Password length is greater than 8 bits!")
     ");
     Form.userpass.focus ();
   return false;
 return true;
   //Verify that the minimum length function minlength (str,length) {if (str.length>=length) return true is met;
 else return false;
   //Determine if the maximum length function maxLength (str,length) {if (str.length<=length) return true is satisfied;
 else return false; } </script> 
<%@ page contenttype= "text/html;charset=gb2312"%>
 
 

The code uses an expression language to display the logged in user information in streets.

3, failure.jsp

Login failure will jump to this interface, the interface code is as follows:

<%@ page contenttype= "text/html;charset=gb2312"%>
 
 

A hyperlink is provided in the code to link to the login interface.

4, Loginbean.java

Complete the login function, which assumes that the username and password are equal to the success of the login.

Package beans;
public class Loginbean {public
  Boolean validate (String username,string userpass) {return
    username.equals ( Userpass);
  }


5, Loginservlet.java

This file completes the control and the main functions can be described as follows:

①. Obtain the user name and password entered by the user from login.jsp;
②. Create Loginbean object, invoke Loginbean method validate;
③. Depending on the result returned by the method, select Success.jsp or failure.jsp to respond to the user.

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 Respon
  SE) throws Servletexception, IOException {doPost (request,response); public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioexceptio
    n {///Get user ID and password entered by users String username = request.getparameter ("username");
    String Userpass = Request.getparameter ("Userpass");
    Create model object Loginbean Loginbean = new Loginbean ();
    Invoke the business method to validate Boolean B = Loginbean.validate (Username,userpass);
    The file to turn to String forward; If the login is successful, write the username into session and turn to success.jsp,//Otherwise turn to failure.jsp if (b) {//get session HttpSession session = (Ht
    Tpsession) Request.getsession (true); Save user name to session session.setattribute ("username", username);
    Target turn file is success.jsp forward = "success.jsp";
    }else{//target turn file is failure.jsp forward = "failure.jsp";
    }//Get Dispatcher object RequestDispatcher Dispatcher = Request.getrequestdispatcher (forward);
  Complete the Jump Dispatcher.forward (request,response);

 }
}

In the code, the user information of the logged-in user is saved in the session, which is also handled in the actual application.

6, Web.xml

The main code is the configuration of the servlet, with the following code:

<?xml version= "1.0" encoding= "UTF-8"?> <web-app version=
"2.4" xmlns= "http://java.sun.com/xml/ns/"
  Java ee "
  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's my Java component</description>
 <display-name>this is the display name of M Y-java 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 your JSP programming.

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.