定義一個簡單的JAVABEAN;
定義一個簡單的JAVABEAN;
我是用exlipse編寫的
我是用exlipse編寫的
mysql
mysql
public class User {
private int id;
private String username;
private String password;
private String realName;
private String sex;
private String school;
private String address;
private String email;
private String phone;
public User(String username
this
this
}
public User() {
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this
}
public int getId() {
return id;
}
public void setId(int id) {
this
}
}
package model;
public class User {
private int id;
private String username;
private String password;
private String realName;
private String sex;
private String school;
private String address;
private String email;
private String phone;
public User(String username
this
this
}
public User() {
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this
}
public int getId() {
return id;
}
public void setId(int id) {
this
}
}
<PRE class=html name=
<PRE class=html name=
<!DOCTYPE hibernate
<hibernate
<class name=
<id name=
<generator class=
</id>
<property name=
<property name=
<property name=
<property name=
<property name=
<property name=
<property name=
<property name=
</class>
</hibernate
<PRE class=html name=
<PRE class=html name=
<PRE class=html name=
<PRE class=html name=
import java
import javax
import javax
import javax
import org
import org
public class SessionFactoryListener implements ServletContextListener {
public static final String SESSION_FACTORY_KEY =
public static final String CONFIG_FILE_KEY =
@Override
public void contextInitialized(ServletContextEvent event) {
ServletContext sc = event
String filename = sc
SessionFactory sf = null;
if (filename == null) {
Configuration config = new Configuration(nfigure();
sf = config
} else {
Configuration config = new Configuration(nfigure(new File(sc
sf = config
}
if (sf != null) {
sc
}
}
@Override
public void contextDestroyed(ServletContextEvent event) {
ServletContext sc = event
SessionFactory sf = (SessionFactory) sc
if (sf != null)
sf
}
}</PRE>
<PRE class=java name=
<PRE class=java name=
<PRE class=java name=
<PRE class=java name=
import java
import java
import javax
import javax
import javax
import javax
import org
import org
import org
import view
import model
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID =
private static final String USERNAME_PARAMETER =
private static final String PASSWORD_PARAMETER =
private static final String SEX_PARAMETER=
private static final String REALNAME_PARAMETER=
private static final String SCHOOL_PARAMETER=
private static final String ADDRESS_PARAMETER=
private static final String EMAIL_PARAMETER=
@Override
protected void doPost(HttpServletRequest req
throws ServletException
SessionFactory sf = (SessionFactory) this
Session session = null;
try {
session = sf
User user=new User();
String username=new String(req
user
user
String sex=new String(req
user
String realName=new String(req
user
String school=new String(req
user
String address=new String(req
user
user
session
session
session
if (user
new LoginServlet()
else
new LoginServlet()
} catch (Exception e) {
e
outputError(resp);
} finally {
session
}
}
private void outputSucceed(HttpServletResponse resp
resp
PrintWriter pw = resp
pw
pw
pw
pw
pw
pw
pw
pw
pw
new SuccessView()
}
public void outputFailed(HttpServletResponse resp) throws IOException {
resp
PrintWriter pw = resp
pw
pw
pw
pw
pw
pw
pw
}
private void outputError(HttpServletResponse resp) throws IOException {
resp
PrintWriter pw = resp
pw
pw
pw
pw
pw
pw
pw
}
}</PRE>
<PRE class=java name=
<PRE class=java name=
<PRE class=java name=
import java
import java
import java
import java
import java
import javax
import javax
import javax
import javax
import javax
import model
import org
import org
import org
import view
public class Select extends HttpServlet {
private static final String USERNAME_PARAMETER=
User user;
ArrayList<String> list
ArrayList<String> list
ArrayList<String> list
ArrayList<String> list
ArrayList<String> list
ArrayList<String> list
ArrayList<String> list
/* (non
* @see javax
*/
@Override
protected void doPost(HttpServletRequest req
throws ServletException
SessionFactory sf = (SessionFactory) this
Session session = null;
try{
session=sf
user=new User();
Query query
query
list
user
Query query
query
list
user
Query query
query
list
user
Query query
query
list
user
Query query
query
list
user
Query query
query
list
user
Query query
query
list
user
resp
PrintWriter pw=resp
pw
pw
pw
pw
pw
pw
pw
pw
pw
pw
pw
pw
}catch(Exception e){
new Select()
}
}
private void faild(HttpServletResponse resp) throws IOException{
resp
PrintWriter pw=resp
pw
pw
pw
pw
pw
pw
}
}</PRE>
<PRE class=java name=
<PRE class=java name=
<PRE class=java name=
<PRE class=java name=
import java
import java
import javax
import javax
import javax
import javax
import javax
import model
import org
import org
import org
import control
import control
public class SuccessView extends HttpServlet {
private static final String USERNAME_PARAMETER=
private static final String PASSWORD_PARAMETER=
public static final String USER_ENTITY=
@Override
protected void doPost(HttpServletRequest req
throws ServletException
SessionFactory sf = (SessionFactory) this
Session session = null;
String username=(String)req
String password=(String)req
try{
if(username
session=sf
Query query = session
query
query
// req
User user=(User)query
HttpSession hs=req
hs
if(query
loginSucceed(resp);
}else{
new LoginServlet()
}
}else{
new LoginServlet()
}
}catch(Exception e){
new LoginServlet()
}finally{
}
}
public void loginSucceed(HttpServletResponse resp)throws IOException{
resp
PrintWriter pw = resp
pw
pw
pw
pw
pw
pw
pw
pw
pw
pw
pw
pw
pw
pw
pw
pw
pw
pw
}
}
</PRE>
</PRE>
</PRE>
<PRE class=java name=
</PRE>
<PRE class=java name=
</PRE>
</PRE>
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26867.html