JSP Learning Notes (v) using JavaBean in-----JSP

Source: Internet
Author: User
Tags insert odbc return string
js| Note 1. This example basically tells us how to call the JavaBean widget in JSP code
2. The advantage of using JavaBean is to simplify the JSP code, the interface code and the logical code are separated from each other, easy for the programmer to view and debug
3. This example requires five documents: login.jsp,test.jsp, Userbean.class
4. First look at the login.jsp


<html>
<center>
<form method=post action="http://127.0.0.1:8000/test.jsp">
username<input type=text name=username>
<br><br>
password<input type=password name=password>
<br><br>
<input type=submit value="注册">
</form>
</center>
</html>




5. The test.jsp code is as follows:



<html>
<jsp:useBean id="hello" class="userbean" scope="session" />
<jsp:setProperty name="hello" property="*" />
your username is:<jsp:getProperty name="hello" property="username"/>
<br><br>
your password is:<jsp:getProperty name="hello" property="password"/>
<br><br>
<%
out.println(hello.insert());
%>
</html>


6. The JavaBean widget Userbean.java code is as follows:
import java.sql.*;
public class userbean
{
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;
}
public String insert()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection dbcon=DriverManager.getConnection("jdbc:odbc:test","sa","");
PreparedStatement stat=dbcon.prepareStatement(
"insert login values(?,?)");
stat.setString(1,username);
stat.setString(2,password);
stat.executeUpdate();
return "success";
}
catch(Exception e)
{
System.out.println(e);
return e.toString();
}
}
}

7. The configuration method is as follows:



Login,test placed in the public_html of the Java EE, Userbean.class placed in the j2ee\lib\classes

















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.