定义一个简单的JAVABEAN;
定义一个简单的JAVABEAN;
我是用exlipse编写的这里没有导包的功能只能把内容列这里了用到的技术有:servletehibernate
我是用exlipse编写的这里没有导包的功能只能把内容列这里了用到的技术有:servletehibernate
mysql用HTML编写的映射文档;
mysql用HTML编写的映射文档; 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 String password) {
thisusername = username;
thispassword = password;
}
public User() {
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
thisusername = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
thispassword = password;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
thisrealName = realName;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
thissex = sex;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
thisschool = school;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
thisaddress = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
thisemail = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
thisphone = phone;
}
public int getId() {
return id;
}
public void setId(int id) {
thisid = id;
}
}
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 String password) {
thisusername = username;
thispassword = password;
}
public User() {
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
thisusername = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
thispassword = password;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
thisrealName = realName;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
thissex = sex;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
thisschool = school;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
thisaddress = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
thisemail = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
thisphone = phone;
}
public int getId() {
return id;
}
public void setId(int id) {
thisid = id;
}
}
<PRE class=html name=code>//建立User类到数据库的映射</PRE>
<PRE class=html name=code><?xml version= encoding=utf?>
<!DOCTYPE hibernatemapping PUBLIC
//Hibernate/Hibernate Mapping DTD //EN
mappingdtd>
<hibernatemapping package=model>
<class name=User table=user>
<id name=id>
<generator class=identity />
</id>
<property name=username length= notnull=true unique=true/>
<property name=password length= notnull=true />
<property name=realName notnull=true />
<property name=sex notnull=true />
<property name=school />
<property name=address />
<property name=email />
<property name=phone />
</class>
</hibernatemapping></PRE>
<PRE class=html name=code> </PRE>
<PRE class=html name=code> </PRE>
<PRE class=html name=code>//用监听器实现hibernate的配置</PRE>
<PRE class=html name=code><PRE class=java name=code>package control;
import javaioFile;
import javaxservletServletContext;
import javaxservletServletContextEvent;
import javaxservletServletContextListener;
import orghibernateSessionFactory;
import orghibernatecfgConfiguration;
public class SessionFactoryListener implements ServletContextListener {
public static final String SESSION_FACTORY_KEY = hibernate_session_factory;
public static final String CONFIG_FILE_KEY = configurationfile;
@Override
public void contextInitialized(ServletContextEvent event) {
ServletContext sc = eventgetServletContext();
String filename = scgetInitParameter(CONFIG_FILE_KEY);
SessionFactory sf = null;
if (filename == null) {
Configuration config = new Configuration(nfigure();
sf = configbuildSessionFactory();
} else {
Configuration config = new Configuration(nfigure(new File(scgetRealPath(filename)));
sf = configbuildSessionFactory();
}
if (sf != null) {
scsetAttribute(SESSION_FACTORY_KEY sf);
}
}
@Override
public void contextDestroyed(ServletContextEvent event) {
ServletContext sc = eventgetServletContext();
SessionFactory sf = (SessionFactory) scgetAttribute(SESSION_FACTORY_KEY);
if (sf != null)
sfclose();
}
}</PRE>
<PRE class=java name=code> </PRE>
<PRE class=java name=code>//如下代码实现了接收请求以及做出的部分回应写得有点乱功能没有独立开不过不想改了;</PRE>
<PRE class=java name=code> </PRE>
<PRE class=java name=code><PRE class=java name=code>package control;
import javaioIOException;
import javaioPrintWriter;
import javaxservletServletException;
import javaxservlethttpHttpServlet;
import javaxservlethttpHttpServletRequest;
import javaxservlethttpHttpServletResponse;
import orghibernateQuery;
import orghibernateSession;
import orghibernateSessionFactory;
import viewSuccessView;
import modelUser;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = L;
private static final String USERNAME_PARAMETER = username;
private static final String PASSWORD_PARAMETER = password;
private static final String SEX_PARAMETER=sex;
private static final String REALNAME_PARAMETER=realName;
private static final String SCHOOL_PARAMETER=school;
private static final String ADDRESS_PARAMETER=address;
private static final String EMAIL_PARAMETER=email;
@Override
protected void doPost(HttpServletRequest req HttpServletResponse resp)
throws ServletException IOException {
SessionFactory sf = (SessionFactory) thisgetServletContext()getAttribute(SessionFactoryListenerSESSION_FACTORY_KEY);
Session session = null;
try {
session = sfopenSession();
User user=new User();
String username=new String(reqgetParameter(USERNAME_PARAMETER)getBytes(ISO)UTF);
usersetUsername(username);
usersetPassword(reqgetParameter(PASSWORD_PARAMETER));
String sex=new String(reqgetParameter(SEX_PARAMETER)getBytes(ISO)UTF);
usersetSex(sex);
String realName=new String(reqgetParameter(REALNAME_PARAMETER)getBytes(ISO)UTF);
usersetRealName(realName);
String school=new String(reqgetParameter(SCHOOL_PARAMETER)getBytes(ISO)UTF);
usersetSchool(school);
String address=new String(reqgetParameter(ADDRESS_PARAMETER)getBytes(ISO)UTF);
usersetAddress(address);
usersetEmail(reqgetParameter(EMAIL_PARAMETER));
sessionbeginTransaction();
sessionsave(user);
sessiongetTransaction(mit();
if (usergetUsername()trim()length()!=)
new LoginServlet()outputSucceed(resp user);
else
new LoginServlet()outputFailed(resp);
} catch (Exception e) {
eprintStackTrace();
outputError(resp);
} finally {
sessionclose();
}
}
private void outputSucceed(HttpServletResponse resp User user) throws Exception {
respsetContentType(text/html; charset=UTF);
PrintWriter pw = respgetWriter();
pwprintln(<html>);
pwprintln(<body>);
pwprintln(<fieldset>);
pwprintln(<legend>用户信息</legend>);
pwprintln(<label>您好:</label> + usergetUsername() + <br/>);
pwprintln(恭喜您注册成功!);
pwprintln(</fieldset>);
pwprintln(</body>);
pwprintln(</html>);
new SuccessView()loginSucceed(resp);
}
public void outputFailed(HttpServletResponse resp) throws IOException {
respsetContentType(text/html; charset=UTF);
PrintWriter pw = respgetWriter();
pwprintln(<html>);
pwprintln(<body>);
pwprintln(无效的用户名或密码!);
pwprintln(<hr/>);
pwprintln(<a href=\l\>返回首页</a>);
pwprintln(</body>);
pwprintln(</html>);
}
private void outputError(HttpServletResponse resp) throws IOException {
respsetContentType(text/html; charset=UTF);
PrintWriter pw = respgetWriter();
pwprintln(<html>);
pwprintln(<body>);
pwprintln(用户名重复请重新输入!);
pwprintln(<hr/>);
pwprintln(<a href=\l\>返回首页</a>);
pwprintln(</body>);
pwprintln(</html>);
}
}</PRE>
<PRE class=java name=code> </PRE>
<PRE class=java name=code>//查询代码我写得有点复杂肯定有简单的方式大家多想想;
<PRE class=java name=code>package control;
import javaioIOException;
import javaioPrintWriter;
import javasqlResultSet;
import javautilArrayList;
import javautilList;
import javaxservletServletException;
import javaxservlethttpHttpServlet;
import javaxservlethttpHttpServletRequest;
import javaxservlethttpHttpServletResponse;
import javaxservlethttpHttpSession;
import modelUser;
import orghibernate* ;
import orghibernateSession;
import orghibernateSessionFactory;
import viewSuccessView;
public class Select extends HttpServlet {
private static final String USERNAME_PARAMETER=username;
User user;
ArrayList<String> list=null;
ArrayList<String> list=null;
ArrayList<String> list=null;
ArrayList<String> list=null;
ArrayList<String> list=null;
ArrayList<String> list=null;
ArrayList<String> list=null;
/* (nonJavadoc)
* @see javaxservlethttpHttpServlet#doPost(javaxservlethttpHttpServletRequest javaxservlethttpHttpServletResponse)
*/
@Override
protected void doPost(HttpServletRequest req HttpServletResponse resp)
throws ServletException IOException {
SessionFactory sf = (SessionFactory) thisgetServletContext()getAttribute(SessionFactoryListenerSESSION_FACTORY_KEY);
Session session = null;
try{
session=sfopenSession();
user=new User();
Query query=sessioncreateQuery(select username from User where username=?);
querysetParameter( reqgetParameter(USERNAME_PARAMETER ));
list=(ArrayList<String>)querylist();
usersetUsername(listget());
Query query=sessioncreateQuery(select realName from User where username=?);
querysetParameter( reqgetParameter(USERNAME_PARAMETER ));
list=(ArrayList<String>)querylist();
usersetRealName(listget());
Query query=sessioncreateQuery(select sex from User where username=?);
querysetParameter( reqgetParameter(USERNAME_PARAMETER ));
list=(ArrayList<String>)querylist();
usersetSex(listget());
Query query=sessioncreateQuery(select school from User where username=?);
querysetParameter( reqgetParameter(USERNAME_PARAMETER ));
list=(ArrayList<String>)querylist();
usersetSchool(listget());
Query query=sessioncreateQuery(select address from User where username=?);
querysetParameter( reqgetParameter(USERNAME_PARAMETER ));
list=(ArrayList<String>)querylist();
usersetAddress(listget());
Query query=sessioncreateQuery(select email from User where username=?);
querysetParameter( reqgetParameter(USERNAME_PARAMETER ));
list=(ArrayList<String>)querylist();
usersetEmail(listget());
Query query=sessioncreateQuery(select phone from User where username=?);
querysetParameter( reqgetParameter(USERNAME_PARAMETER ));
list=(ArrayList<String>)querylist();
usersetPhone(listget());
respsetContentType(text/html; charset=UTF);
PrintWriter pw=respgetWriter();
pwprintln(<html>);
pwprintln(<body>);
pwprintln(您所查询的+usergetRealName()+个人信息+<br/>);
pwprintln(用户名+usergetUsername()+<br/>);
pwprintln(真实姓名+usergetRealName()+<br/>);
pwprintln(性别:+usergetSex()+<br/>);
pwprintln(学校:+usergetSchool()+<br/>);
pwprintln(家庭住址:+usergetAddress()+<br/>);
pwprintln(邮箱:+usergetEmail()+<br/>);
pwprintln(联系电话+usergetPhone()+<br/>);
pwprintln(</body>);
pwprintln(<html>);
}catch(Exception e){
new Select()faild(resp);
}
}
private void faild(HttpServletResponse resp) throws IOException{
respsetContentType(text/html; charset=UTF);
PrintWriter pw=respgetWriter();
pwprintln(<html>);
pwprintln(<body>);
pwprintln(您查询的信息不存在!);
pwprintln(<a href=\suceessdo\>返回</a>);
pwprintln(</body>);
pwprintln(</html>);
}
}</PRE>
<PRE class=java name=code> </PRE>
<PRE class=java name=code> </PRE>
<PRE class=java name=code>//只是个登录界面比较简单本来我想做个MVC结构的可是那要好好的组织代码花得时间太长只好放弃</PRE>
<PRE class=java name=code><PRE class=java name=code>package view;
import javaioIOException;
import javaioPrintWriter;
import javaxservletServletException;
import javaxservlethttpHttpServlet;
import javaxservlethttpHttpServletRequest;
import javaxservlethttpHttpServletResponse;
import javaxservlethttpHttpSession;
import modelUser;
import orghibernateQuery;
import orghibernateSession;
import orghibernateSessionFactory;
import controlLoginServlet;
import controlSessionFactoryListener;
public class SuccessView extends HttpServlet {
private static final String USERNAME_PARAMETER=username;
private static final String PASSWORD_PARAMETER=password;
public static final String USER_ENTITY=user;
@Override
protected void doPost(HttpServletRequest req HttpServletResponse resp)
throws ServletException IOException {
SessionFactory sf = (SessionFactory) thisgetServletContext()getAttribute(SessionFactoryListenerSESSION_FACTORY_KEY);
Session session = null;
String username=(String)reqgetParameter(USERNAME_PARAMETER);
String password=(String)reqgetParameter(PASSWORD_PARAMETER);
try{
if(usernametrim()length()!=){
session=sfopenSession();
Query query = sessioncreateQuery(from User where username= ? and password = ?);
querysetParameter( username);
querysetParameter( password);
// reqsetAttribute(usernameUSERNAME_PARAMETER );
User user=(User)queryuniqueResult();
HttpSession hs=reqgetSession();
hsgetAttribute(USER_ENTITY);
if(queryuniqueResult() != null){
loginSucceed(resp);
}else{
new LoginServlet()outputFailed(resp);
}
}else{
new LoginServlet()outputFailed(resp);
}
}catch(Exception e){
new LoginServlet()outputFailed(resp);
}finally{
}
}
public void loginSucceed(HttpServletResponse resp)throws IOException{
respsetContentType(text/html; charset=UTF);
PrintWriter pw = respgetWriter();
pwprintln(<html>);
pwprintln(<body>);
pwprintln(<label>您好:</label> +欢迎来到网络通讯录!+ <br/>);
pwprintln(<hr/>);
pwprintln(<a href=\l\>返回首页</a>);
pwprintln(<form method=\post\ action=\select\);
pwprintln(<table>);
pwprintln(<tr>);
pwprintln(<td>);
pwprintln(<input type=\submit\ name=\select\ value=\查询\>);
pwprintln(</td>);
pwprintln(<td>);
pwprintln(<input name=\username\ type=\text\ maxlength=\\>);
pwprintln(</td>);
pwprintln(</tr>);
pwprintln(</form>);
pwprintln(</body>);
pwprintln(</html>);
}
}
</PRE>
</PRE>
</PRE>
<PRE class=java name=code>还有HTML做的界面在下面的文章里将给大家列出来</PRE>
</PRE>
<PRE class=java name=code>
</PRE>
</PRE>