把JSP传数据的到JAVABEAN
jsp文件如下:
<%@ page contentType="text/html; charset=GBK" %>
<%@include file="Welcome.jsp" %>
<html>
<head>
<title>
Login
</title>
</head>
<body bgcolor="#ffffff">
<h3>
Login page!
</h3>
<form method="post" name="LoginForm" action="loginservlet">
yourname:<input type="text" name="username"><br>
password:<input type="text" name="password"><br><br>
<input type="submit" name="Submit" value="Submit">
<input type="reset" value="Reset">
</form>
<a href="Register.jsp">Register</a>
</body>
</html>
javaBean文件如下:
package myjava.bean;
import java.util.*;
tDatabase.*;
import java.sql.*;
public class UserBean extends ConnectDatabase{
private String userName;
private String password;
private Hashtable customList = new Hashtable();
public UserBean() {
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public UserBean(String username, String password) {
this.userName = username;
this.password = password;
}
public void add(String username,String password){
ResultSet rs=null;
javaservlet和jsp的比较 connect();
String sql="insert into customer values('',"+this.userName+","+this.password+")";
try{
data(sql);
} catch (SQLException ex) {
}
}
/*public boolean check(){
int count=0;
connect();
String sql="select username,password from customer where username='"+this.userName+"' and password='"+this.password+"'";
ResultSet rs=null;
try {
rs = data(sql);
if (rs.next()){
return true;
}
else{
return false;
}
}
catch (SQLException ex) {
return false;
}
}
*/
public boolean check(){
boolean re=true;
int count=0;
connect();
String sql="select count(*) as a from customer where username='"+userName+"' and password='"+password+"'";
ResultSet rs=null;
try {
rs = data(sql);
if (rs.next()){
Int("a");
System.out.println(count);
}
if (count==0){
return false;
}
else{
return re;
}
}
catch (SQLException ex) {
return false;
}
}
public void remove(){
connect();
String sql="delete from customer where username='"+userName+"'";
try {
getdata(sql);
}
catch (SQLException ex) {
ex.printStackTrace();
}
diconnect();
}
public void update(){
connect();
String sql="update from customers set password='"+password+"' where username='"+userName+"'";
try {
getdata(sql);
}
catch (SQLException ex) {
ex.printStackTrace();
}
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
Servlet文件如下:
package myjava.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import myjava.bean.UserBean;
public class LoginServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// response.setContentType(CONTENT_TYPE);
String username = Parameter("username");
String password = Parameter("password");
System.out.println("In the loginservlet!");
UserBean user=new UserBean(username,password);
if(user.check()){
System.out.println(user.check() );
HttpSession Session();
session.setAttribute("user",username);
response.sendRedirect("/Main.jsp");
}
else{
System.out.println("Login false,try again!");
response.sendRedirect("/Login.jsp");
}
}
} 5号仓库 www.5hck
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论