JSP + servlet + JavaBean implement the MVC mode of SSH

Source: Internet
Author: User

All the projects that have been working for a long time use the SSH framework. Now, a beautiful girl from the team wants to engage in Java. Let me provide guidance and build a basic framework, the most basic architecture is required. In this way, SSH can only be rolled far away;

The most basic architecture is JSP + servlet + JavaBean, but I still feel uncomfortable. This is not a waste of time. Later I thought about it. By the way, I can design a simple MVC Architecture without framework to achieve the layered effect of the framework:

Here I will use the logon as an example to explain the order v ---> C ----> m

Page: login.html

<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <HTML> 

Configuration File web. xml

<Servlet> <servlet-Name> controlservlet </servlet-Name> <servlet-class> xidian. SL. Equipment. servlet. controlservlet </servlet-class> <! -- The configuration here is the same as the general servlet, but because only one servlet exists in the project, all access requests will access this servlet, therefore, you only need to add the following parameters for different accesses --> <init-param> <! -- Address of the page request --> <param-Name> adminlogin </param-Name> <! -- Process the background action address of the action --> <param-value> xidian. SL. equipment. action. loginaction </param-value> </init-param> <param-Name> piclistaction </param-Name> <param-value> xidian. SL. equipment. action. piclistaction </param-value> </init-param> <param-Name> newlistaction </param-Name> <param-value> xidian. SL. equipment. action. newlistaction </param-value> </init-param> </servlet> <servlet-mapping> <servlet-Name> controlservlet </servlet-Name> <URL-pattern> *. action </url-pattern> </servlet-mapping>

Servlet acting as control layer: xidian. SL. Equipment. servlet. controlservlet (important)

Package xidian. SL. equipment. servlet; import Java. io. ioexception; import javax. servlet. servletexception; import javax. servlet. HTTP. httpservlet; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; import xidian. SL. equipment. action. interfaces. action; import xidian. SL. equipment. actionfactory. actionfactory; public class controlservlet extends httpservlet {/*** any request will be sent to this servlet. this servlet acts as the C (control layer) in MVC mode) */Private Static final long serialversionuid = 1l; @ override protected void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {dopost (request, response );} @ override protected void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {/*** get the Request Path of the current servlet **/string pathname = request. getservletpath (); // system. out. println ("pathname:" + pathname);/*** get the requested action name **/INT Index = pathname. indexof (". "); string actionname = pathname. substring (1, index); // system. out. println (actionname);/*** get the runtime parameter **/string actionclassname = This. getinitparameter (actionname); // system. out. println ("actionclassname" + actionclassname);/*** get the action object **/Action action = actionfactory. getactionfactory (). getaction (actionclassname); // system. out. println ("action" + action);/*** execute the action to get the URL path to be returned **/string url = action.exe cute (request, response ); if (url = NULL) {request. getrequestdispatcher ("error. JSP "). forward (request, response);} else {request. getrequestdispatcher (URL ). forward (request, response );}}}

Action: interface-oriented programming. It provides a unified action interface with a method called execute (). It simulates struts1.x.

Package xidian. SL. equipment. action. interfaces; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; public interface action {/*** all specific actions implement this interface * @ Param request object * @ Param response object * @ return: result page */Public String execute (httpservletrequest request, httpservletresponse response );}

Loginaction:

Package xidian. SL. equipment. action; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; import xidian. SL. equipment. action. interfaces. action; import xidian. SL. equipment. dao. impl. admindaoimpl; import xidian. SL. equipment. dao. interfaces. admindao; public class loginaction implements action {@ override Public String execute (httpservletrequest request, httpservletrespons E response) {/*** get the corresponding value from the request **/string username = request. getparameter ("username"); string Password = request. getparameter ("password");/*** instantiate the DaO layer, but this is still not perfect, it is best to write a dependent injection class **/admindao = new admindaoimpl (); /*** returns the searched two-dimensional array **/string [] [] DATA = admindao. findadmin (username, password); If (Data = NULL | (Data! = NULL & data. length = 0) {// The array is null, indicating that the account does not exist. Return "admin/login.html" ;}else {return "admin/index.html ";}}}

In the implementation class loginaction, admindao is instantiated for persistence. For convenience, new is directly used for instantiation, but hard encoding brings coupling. for improvement, see simulate Spring IoC.
Http://www.cnblogs.com/shenliang123/archive/2012/05/10/2494412.html to discuss the simple factory mode has a simple implementation, you can improve to make it more perfect

Admindao:

Package xidian. SL. equipment. dao. interfaces; public interface admindao {/*** find the corresponding user based on the user name and password **/Public String [] [] findadmin (string username, string password );}

Admindaoimpl:

package xidian.sl.equipment.dao.impl;import xidian.sl.equipment.dao.interfaces.AdminDAO;import xidian.sl.equipment.util.DbConn;public class AdminDAOImpl implements AdminDAO{    @Override    public String[][] findAdmin(String username, String password) {        String sql = "select * from admin as ad where ad.aId = '"+username+"' and ad.aPassword = '"+password+"'";        String[][] data = DbConn.query(sql);        return data;    }}

This implementation class uses the encapsulated database operation class, see: http://www.cnblogs.com/shenliang123/archive/2012/05/10/2494874.html

The database design is relatively simple: you only need the primary key ID, user account: username, account password: Password

This basically achieves the MVC effect. You can insert a service layer between Dao and action based on the complexity of the project.

 

 

 

 

 

 

 

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.