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