An example analysis of MVC pattern in Java Web application

Source: Internet
Author: User
Tags java web

Combined with six basic mass properties

Availability of:

Abnormal

Modifiable:

1. maintain semantic consistency, cohesion low coupling

2. maintain the existing interface,Login relies on the loginiservice interface,loginservice dependent Ilogdao

Performance: No

Security:

Authenticating a user

Ease of use: not currently

analysis of specific function modules MVC Design implementations (such as login, user permissions, and other functional modules)

specific application or analysis in a real-world project Application of MVC design patterns to specific websites (examples are required)

Login

Public String Execute () throws Exception

{

String[] Request=loginservice.userpan (user, pwd, randcode);

Req.getsession (). SetAttribute ("username", user);

HttpServletResponse response = Servletactioncontext. GetResponse ();

Response.setcontenttype ("Text/html;charset=utf-8");

Response.setcharacterencoding ("UTF-8");//Prevent garbled information from popping up

PrintWriter out = Response.getwriter ();

if (Request[0].equals ("Error"))

{

Out.print ("<script>alert ('" +request[1]+ "');</script>");

Out.flush ();

Out.close ();

return "Error";

}

return "Success";

}

Loginiserivece

Public String[] Userpan (String user,string pwd,string randcode);

Loginservice

Enter user name, password, verification code, return (ID,MSG) error and error message

Public String[] Userpan (String user,string pwd,string randcode)

{

string[] msg = new string[2];

String bm= "";

HttpServletRequest Req=servletactioncontext. getrequest ();

String srand= (String) req.getsession (). getattribute ("Rand");//session.setattribute ("Rand", Srand);

Msg[0]= "Error";

msg[1]= "Unknown error";

Try

{

if (!srand.equals (Randcode))

{

Msg[0]= "Error";

msg[1]= "Verification Code Error";

return msg;

}

}

Catch (Exception e) {

Msg[0]= "Error";

msg[1]= "Verification Code Error";

return msg;

}

Try {

Log Result=logdao.findbyid (user);

if (result==null)

{

Msg[0]= "Error";

msg[1]= "account does not exist";

return msg;

}

if (!result.getpassword (). Equals (PWD))

{

Msg[0]= "Error";

msg[1]= "Password Error";

return msg;

}

BM=RESULT.GETBM ();

}

Catch (Exception e) {

Msg[0]= "Error";

msg[1]= "Account password Error";

return msg;

}

Req.getsession (). SetAttribute ("BM", BM);

Msg[0]= "Success";

return msg;

}

Ilogindao

Public Log FindByID (java.lang.String ID);

Logindao

Public Log FindByID (java.lang.String ID)

{

log. Debug ("Getting Log instance with ID:" + ID);

Try

{

Log instance = (log) gethibernatetemplate (). Get ("Interdao.dao.Log",

ID);

return instance;

} catch (RuntimeException re)

{

log. Error ("Get Failed", re);

throw re;

}

}

Log

Public   class  log   Extends  abstractlog implements  java.io.serializable

{

 

//constructors

 

/** default constructor */

public  log ()

{

}

 

/** Minimal constructor */

Public  log (string Username, string password, string role)

{

super (username, password, role);

}

 

/** Full constructor */

public  log (string username, string Password, string role, String bm)

{

Super (username, password, role, BM);

}

 

}

Instance analysis of the MVC pattern in a Java Web application

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.