JSP learning diary (1) use of JavaBean and MVC of JavaBean + Servlet

Source: Internet
Author: User

The running mechanism of JSP is much more convenient than that of. Net's webform architecture. You only need to configure Java SDK and tomcat to run the embedded code in JSP. This is a bit similar to the previous ASP.

Later, in order to reuse the logic code and add too many embedded page Java code, which is difficult to manage, a two-tier architecture of JSP + JavaBean emerged. It is to extract the logic code and put it in the class, and then call the class on the JSP page. The calss function is similar to the webform background code, but it is completely different in nature; the background code of webform is a server control that can directly control the ASPX page, while the JavaBean is used to transmit data or merge and process data. Then it is displayed in JSP. The two layers of the JSP + JavaBean mode are clearly divided.

Define JavaBean:

Package javaweb; public class JavaBean {private string username; private string password; Public void setusername (string username) {This. username = username;} public void setpassword (string password) {This. password = password;} Public String GetUserName () {return username;} Public String GetPassword () {return password ;}} construct login. JSP and Welcom. JSP:
login.jsp:
<% @ Page Language = "Java" Import = "Java. util. * "pageencoding =" UTF-8 "%> <HTML> <center> <form method = post action =" wellcom. JSP "> username <input type = text name = username> <br> password <input type = password name = PASSWORD> <br> <input type = submit value = "register"> </form> </center> 
welcom.jsp:

<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %>
<HTML>
<JSP: usebean id = "hello" class = "javaweb. JavaBean" Scope = "session"/>
<JSP: setproperty name = "hello" property = "*"/>
<%
// Hello. Username = "myname ";
%>
Your username is: <% = Hello. GetUserName () %>
<Br>
Your password is: <JSP: getproperty name = "hello" property = "password"/>
<Br>

</Html>

JSP + JavaBean + servlet (commonly known as MVC structure)

The previous Javabean is divided into two layers. Now, adding a servlet is divided into three layers. Servlet is a bit like the routing function of the Asp.net MVC framework, that is, the page console, responsible for controlling page value transfer and transmission. The above Code remains unchanged. Add one more servlet:

Import Java. io. *; import javax. servlet. *; import javax. servlet. HTTP. *; import COM. huanlin. userinfobean; public class helloservlet2 extends httpservlet {public void Service (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {response. setcontenttype ("text/html; charset = GBK"); Request. setcharacterencoding ("GBK"); // create the an object JavaBean bean = new JavaBean (); bean. setusername (request. getparameter ("username "). tostrng (); httpsession session = request. getsession (); Session. setattribute ("userinfo", userinfo); // go to the specified webpage response. sendredirect ("Welcom. JSP ")}}

Of course you also need to set the Web. xml below the WEB-INF to set the route:

<servlet>        <servlet-name>            login        </servlet-name>        <servlet-class>            javaweb.javabean        </servlet-class>    </servlet>    <servlet-mapping>        <servlet-name>login</servlet-name>        <url-pattern>/login.do</url-pattern>    </servlet-mapping>
Then modify the action of the login. jsp label form = "/login. Do"
This simple MVC is implemented.
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.