Javabean uses the mvc of JavaBean + servlet

Source: Internet
Author: User

Javabean uses the mvc of javabean + servlet

Set web. xml under 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>

Login. jsp tutorial

<% @ 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>
</Html>

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;
}
}

 

<% @ 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)

Import java. io .*;
Import javax. servlet .*;
Import javax. servlet. http .*;
Import com. huanlin. userinfobean;

Public class helloservlet2 extends https tutorial ervlet {
Public void service (httpservletrequest request,
Httpservletresponse response)
Throws servletexception, ioexception {


Response. setcontenttype ("text/html; charset = gbk ");
Request. setcharacterencoding ("gbk ");

// Create a javabean 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 ")
}
}

In order to reuse the logic code section 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 can directly control the server controls on the asp tutorial page x, while the javabean is used to transmit data or merge and process data, which is then displayed in jsp, the two layers of jsp + javabean mode have a clear division of labor

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.