Make your own MVC framework (take the login page for example)

Source: Internet
Author: User
Tags xmlns
Make your own MVC framework (take the login page for example)
1, first set up a class named Login class file, in the class to build a method named login, to this method to pass two values, one for the user name, and the other for the password.
The Login.java class code is as follows: (model layer)


Package com.biz;  My package is in com.biz public
class Login {public
	boolean login (String uname,string upass) {
		Boolean b = false;
		if ("123". Equals (uname) && "123". Equals (UPass)) {
			b= true;
		} Here is to make a judgment
		return B;
	}
}




2, after building the method class, then set up an action (Controller), set up a class file named Loginaction, where the main function is to receive the parameters on the page, and then processed, put to login (String uname,string UPass) method.
Then the judgment is then transferred to the different pages according to the result of the judgment.
The Loginaction.java class code is as follows: (Controller)


Package com.action;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import com.action.imp.Action;
Import Com.biz.Login;


public class loginaction{
	Private Login login = new login ();
	Public String Excute (httpservletrequest request,
			httpservletresponse response) {
		string uname= Request.getparameter ("uname");
		String UPass = Request.getparameter ("UPass");
		Boolean B = false;
		B=login.login (uname, upass);
		if (b) {
			request.setattribute ("user", uname);
			return "login_success.jsp";
		} else{
			request.setattribute ("Error", "User name or password is wrong!");
			return "index.jsp";}}
}




3, establish the control layer and then set up the view layer, that is, the view layer. is the JSP page
Create a page with a login box here




<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%> <% String Path = Request.getcontextpath ()
	; String basepath = request.getscheme () + "://" + request.getservername () + ":" + request.getserverport () + path + "/"
; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >  





4, there is this login page, also need a display login successful page
Simply create a login_success.jsp page here
The code is as follows:


<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%>
<%
String Path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";
%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >






5, with these is not feel is finished. If you think it's over, go get the program published and then accessed in the browser with the address: http://localhost:8080/Project name/login.do
You can access the.
But this has to be problematic, because it's just not enough. Then there must be a 404 error in Access!
A servlet dedicated to intercepting *.do files must be created to intercept the login.do and then process
The Controller.java code is as follows:


Package com.action;
Import java.io.IOException;
Import Java.util.HashMap;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import com.action.imp.Action; /** * Custom MVC framework: servlet implementation-based controller */public class Controller extends HttpServlet {private HashMap actionmap = new HashMap
	();
	 /** * Servlet initialization method.
		*/public void init () throws servletexception {ActionMap = new HashMap ();  Actionmap.put ("Login", new Loginaction ()); Here the "Login" and form form in the action= "" corresponding login.do must be consistent//new loginaction () is corresponding to the loginaction, that is, the control layer}/** * According to the path to determine by which act
	 Ion to perform the operation.  */Private Action Determinactionbypath (string path) {string actionname = path.substring (Path.lastindexof ('/') + 1,  Path.length ()-3); 
		Here is the truncated string, is to put the address of the login.do intercept Action ret = (action) actionmap.get (ActionName);
	return ret; } public void Doget (HttpServletRequest request, httpservletresponse response)
			Throws Servletexception, IOException {//get path, such as: http://localhost:8080/epet/ShowBaby.do String path = REQUEST.G	
		Etservletpath ();				
		Find the Action action action = (action) this.determinactionbypath (path);	
		Execution action String Resultview = Action.excute (request,response);
		Control page Steering if (null!=resultview) {request.getrequestdispatcher (Resultview). Forward (request, response); }} public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioexcepti
	On {this.doget (request, response);
} private static final long serialversionuid = -2269577614162154800l; }



6, next configuration in Web. xml


<?xml version= "1.0" encoding= "UTF-8"?>
<web-app version= "3.0" 
	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/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd ">
  <display-name></ display-name>
  <servlet>
    <servlet-name>Controller</servlet-name>
    < servlet-class>com.action.controller</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Controller</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</ welcome-file>
  </welcome-file-list>
</web-app>



WEB.XM you have to make sure that your controller class has the correct address.
7, finish this, and now it's done. Publish the program, and then access the Address http://localhost:8080/project name/login.do
And then it worked out.

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.