知识大全 一个简单JAVA网络通讯录
Posted 知
篇首语:人有恒心万事成,人无恒心万事崩。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 一个简单JAVA网络通讯录相关的知识,希望对你有一定的参考价值。
一个简单JAVA网络通讯录 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
定义一个简单的JAVABEAN;
定义一个简单的JAVABEAN; 我是用exlipse编写的 这里没有导包的功能 只能把内容列这里了 用到的技术有:servlete hibernate
我是用exlipse编写的 这里没有导包的功能 只能把内容列这里了 用到的技术有:servlete hibernate 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)
this username = username;
this password = password;
public User()
public String getUsername()
return username;
public void setUsername(String username)
this username = username;
public String getPassword()
return password;
public void setPassword(String password)
this password = password;
public String getRealName()
return realName;
public void setRealName(String realName)
this realName = realName;
public String getSex()
return sex;
public void setSex(String sex)
this sex = sex;
public String getSchool()
return school;
public void setSchool(String school)
this school = school;
public String getAddress()
return address;
public void setAddress(String address)
this address = address;
public String getEmail()
return email;
public void setEmail(String email)
this email = email;
public String getPhone()
return phone;
public void setPhone(String phone)
this phone = phone;
public int getId()
return id;
public void setId(int id)
this id = 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)
this username = username;
this password = password;
public User()
public String getUsername()
return username;
public void setUsername(String username)
this username = username;
public String getPassword()
return password;
public void setPassword(String password)
this password = password;
public String getRealName()
return realName;
public void setRealName(String realName)
this realName = realName;
public String getSex()
return sex;
public void setSex(String sex)
this sex = sex;
public String getSchool()
return school;
public void setSchool(String school)
this school = school;
public String getAddress()
return address;
public void setAddress(String address)
this address = address;
public String getEmail()
return email;
public void setEmail(String email)
this email = email;
public String getPhone()
return phone;
public void setPhone(String phone)
this phone = phone;
public int getId()
return id;
public void setId(int id)
this id = id;
<PRE class= name= code >//建立User类到数据库的映射</PRE> <PRE class= name= code ><?xml version= encoding= utf ?>
<!DOCTYPE hibernate mapping PUBLIC
//Hibernate/Hibernate Mapping DTD //EN
mapping dtd >
<hibernate mapping package= model >
<class name= User table= user >
<id name= id >
<generator class= identity />
</id>
<property name= username length= not null= true unique= true />
<property name= password length= not null= true />
<property name= realName not null= true />
<property name= sex not null= true />
<property name= school />
<property name= address />
<property name= email />
<property name= phone />
</class>
</hibernate mapping></PRE> <PRE class= name= code > </PRE> <PRE class= name= code > </PRE> <PRE class= name= code >//用监听器实现hibernate的配置</PRE> <PRE class= name= code ><PRE class=java name= code >package control;
import java io File;
import javax servlet ServletContext;
import javax servlet ServletContextEvent;
import javax servlet ServletContextListener;
import hibernate SessionFactory;
import hibernate cfg Configuration;
public class SessionFactoryListener implements ServletContextListener
public static final String SESSION_FACTORY_KEY = hibernate_session_factory ;
public static final String CONFIG_FILE_KEY = configuration file ;
@Override
public void contextInitialized(ServletContextEvent event)
ServletContext sc = event getServletContext();
String filename = sc getInitParameter(CONFIG_FILE_KEY);
SessionFactory sf = null;
if (filename == null)
Configuration config = new Configuration(nfigure();
sf = config buildSessionFactory();
else
Configuration config = new Configuration(nfigure(new File(sc getRealPath(filename)));
sf = config buildSessionFactory();
if (sf != null)
sc setAttribute(SESSION_FACTORY_KEY sf);
@Override
public void contextDestroyed(ServletContextEvent event)
ServletContext sc = event getServletContext();
SessionFactory sf = (SessionFactory) sc getAttribute(SESSION_FACTORY_KEY);
if (sf != null)
sf close();
</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 java io IOException;
import java io PrintWriter;
import javax servlet ServletException;
import javax servlet HttpServlet;
import javax servlet HttpServletRequest;
import javax servlet HttpServletResponse;
import hibernate Query;
import hibernate Session;
import hibernate SessionFactory;
import view SuccessView;
import model User;
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) this getServletContext() getAttribute(SessionFactoryListener SESSION_FACTORY_KEY);
Session session = null;
try
session = sf openSession();
User user=new User();
String username=new String(req getParameter(USERNAME_PARAMETER) getBytes( ISO ) UTF );
user setUsername(username);
user setPassword(req getParameter(PASSWORD_PARAMETER));
String sex=new String(req getParameter(SEX_PARAMETER) getBytes( ISO ) UTF );
user setSex(sex);
String realName=new String(req getParameter(REALNAME_PARAMETER) getBytes( ISO ) UTF );
user setRealName(realName);
String school=new String(req getParameter(SCHOOL_PARAMETER) getBytes( ISO ) UTF );
user setSchool(school);
String address=new String(req getParameter(ADDRESS_PARAMETER) getBytes( ISO ) UTF );
user setAddress(address);
user setEmail(req getParameter(EMAIL_PARAMETER));
session beginTransaction();
session save(user);
session getTransaction(mit();
if (user getUsername() trim() length()!= )
new LoginServlet() outputSucceed(resp user);
else
new LoginServlet() outputFailed(resp);
catch (Exception e)
e printStackTrace();
outputError(resp);
finally
session close();
private void outputSucceed(HttpServletResponse resp User user) throws Exception
resp setContentType( text/; charset=UTF );
PrintWriter pw = resp getWriter();
pw println( <> );
pw println( <body> );
pw println( <fieldset> );
pw println( <legend>用户信息</legend> );
pw println( <label>您好:</label> + user getUsername() + <br/> );
pw println( 恭喜您注册成功! );
pw println( </fieldset> );
pw println( </body> );
pw println( </> );
new SuccessView() loginSucceed(resp);
public void outputFailed(HttpServletResponse resp) throws IOException
resp setContentType( text/; charset=UTF );
PrintWriter pw = resp getWriter();
pw println( <> );
pw println( <body> );
pw println( 无效的用户名或密码! );
pw println( <hr/> );
pw println( <a href=\\ l\\ >返回首页</a> );
pw println( </body> );
pw println( </> );
private void outputError(HttpServletResponse resp) throws IOException
resp setContentType( text/; charset=UTF );
PrintWriter pw = resp getWriter();
pw println( <> );
pw println( <body> );
pw println( 用户名重复 请重新输入! );
pw println( <hr/> );
pw println( <a href=\\ l\\ >返回首页</a> );
pw println( </body> );
pw println( </> );
</PRE> <PRE class=java name= code > </PRE> <PRE class=java name= code >//查询代码 我写得有点复杂 肯定有简单的方式 大家多想想; <PRE class=java name= code >package control;
import java io IOException;
import java io PrintWriter;
import java sql ResultSet;
import java util ArrayList;
import java util List;
import javax servlet ServletException;
import javax servlet HttpServlet;
import javax servlet HttpServletRequest;
import javax servlet HttpServletResponse;
import javax servlet HttpSession;
import model User;
import hibernate * ;
import hibernate Session;
import hibernate SessionFactory;
import view SuccessView;
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;
/* (non Javadoc)
* @see javax servlet HttpServlet#doPost(javax servlet HttpServletRequest javax servlet HttpServletResponse)
*/
@Override
protected void doPost(HttpServletRequest req HttpServletResponse resp)
throws ServletException IOException
SessionFactory sf = (SessionFactory) this getServletContext() getAttribute(SessionFactoryListener SESSION_FACTORY_KEY);
Session session = null;
try
session=sf openSession();
user=new User();
Query query =session createQuery( select username from User where username=? );
query setParameter( req getParameter(USERNAME_PARAMETER ));
list =(ArrayList<String>)query list();
user setUsername(list get( ));
Query query =session createQuery( select realName from User where username=? );
query setParameter( req getParameter(USERNAME_PARAMETER ));
list =(ArrayList<String>)query list();
user setRealName(list get( ));
Query query =session createQuery( select sex from User where username=? );
query setParameter( req getParameter(USERNAME_PARAMETER ));
list =(ArrayList<String>)query list();
user setSex(list get( ));
Query query =session createQuery( select school from User where username=? );
query setParameter( req getParameter(USERNAME_PARAMETER ));
list =(ArrayList<String>)query list();
user setSchool(list get( ));
Query query =session createQuery( select address from User where username=? );
query setParameter( req getParameter(USERNAME_PARAMETER ));
list =(ArrayList<String>)query list();
user setAddress(list get( ));
Query query =session createQuery( select email from User where username=? );
query setParameter( req getParameter(USERNAME_PARAMETER ));
list =(ArrayList<String>)query list();
user setEmail(list get( ));
Query query =session createQuery( select phone from User where username=? );
query setParameter( req getParameter(USERNAME_PARAMETER ));
list =(ArrayList<String>)query list();
user setPhone(list get( ));
resp setContentType( text/; charset=UTF );
PrintWriter pw=resp getWriter();
pw println( <> );
pw println( <body> );
pw println( 您所查询的 +user getRealName()+ 个人信息 + <br/> );
pw println( 用户名 +user getUsername()+ <br/> );
pw println( 真实姓名 +user getRealName()+ <br/> );
pw println( 性别: +user getSex()+ <br/> );
pw println( 学校: +user getSchool()+ <br/> );
pw println( 家庭住址: +user getAddress()+ <br/> );
pw println( 邮箱: +user getEmail()+ <br/> );
pw println( 联系电话 +user getPhone()+ <br/> );
pw println( </body> );
pw println( <> );
catch(Exception e)
new Select() faild(resp);
private void faild(HttpServletResponse resp) throws IOException
resp setContentType( text/; charset=UTF );
PrintWriter pw=resp getWriter();
pw println( <> );
pw println( <body> );
pw println( 您查询的信息不存在! );
pw println( <a href=\\ suceess do\\ >返回</a> );
pw println( </body> );
pw println( </> );
</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 java io IOException;
import java io PrintWriter;
import javax servlet ServletException;
import javax servlet HttpServlet;
import javax servlet HttpServletRequest;
import javax servlet HttpServletResponse;
import javax servlet HttpSession;
import model User;
import hibernate Query;
import hibernate Session;
import hibernate SessionFactory;
import control LoginServlet;
import control SessionFactoryListener;
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) this getServletContext() getAttribute(SessionFactoryListener SESSION_FACTORY_KEY);
Session session = null;
String username=(String)req getParameter(USERNAME_PARAMETER);
String password=(String)req getParameter(PASSWORD_PARAMETER);
try
if(username trim() length()!= )
session=sf openSession();
Query query = session createQuery( from User where username= ? and password = ? );
query setParameter( username);
query setParameter( password);
// req setAttribute( username USERNAME_PARAMETER );
User user=(User)query uniqueResult();
HttpSession hs=req getSession();
hs getAttribute(USER_ENTITY);
if(query uniqueResult() != 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
resp setContentType( text/; charset=UTF );
PrintWriter pw = resp getWriter();
pw println( <> );
pw println( <body> );
pw println( <label>您好:</label> + 欢迎来到网络通讯录! + <br/> );
pw println( <hr/> );
pw println( <a href=\\ l\\ >返回首页</a> );
pw println( <form method=\\ post\\ action=\\ select\\ );
pw println( <table> );
pw println( <tr> );
pw println( <td> );
pw println( <input type=\\ submit\\ name=\\ select\\ value=\\ 查询\\ > );
pw println( </td> );
pw println( <td> );
pw println( <input name=\\ username\\ type=\\ text\\ maxlength=\\ \\ > );
pw println( </td> );
pw println( </tr> );
pw println( </form> );
pw println( </body> );
pw println( </> );
</PRE>
</PRE> </PRE> <PRE class=java name= code >还有HTML做的界面 在下面的文章里将给大家列出来 </PRE> </PRE> <PRE class=java name= code > </PRE> </PRE>
cha138/Article/program/Java/hx/201311/26867相关参考
用Java编写简单的UDP网络通信程序 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! Java中
利用Java实现串口全双工通讯 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 一个嵌入式系统通常
用C#实现基于TCP协议的网络通讯 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!TCP协议是一个基
基于Socket的Java网络编程集粹 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!其实简单的分析
通讯网络冗余切换试验时,除了任意节点上人为切断一条通信总线外,还可以普遍采用的试验方法,是()
通讯网络冗余切换试验时,除了任意节点上人为切断一条通信总线外,还可以普遍采用的试验方法,是()A、切投通信总线上的任意节点B、拔出一块PC网卡C、断开一根电缆或一个终端匹配器D、切断主运行总线模件的电
通讯网络冗余切换试验时,除了任意节点上人为切断一条通信总线外,还可以普遍采用的试验方法,是()
通讯网络冗余切换试验时,除了任意节点上人为切断一条通信总线外,还可以普遍采用的试验方法,是()A、切投通信总线上的任意节点B、拔出一块PC网卡C、断开一根电缆或一个终端匹配器D、切断主运行总线模件的电
Java串行端口通讯技术 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 了解串行通讯 串行通讯
知识大全 农行手机银行登录显示通讯异常请检查你的网络设置及通讯情况是什么原因
农行手机银行登录显示通讯异常请检查你的网络设置及通讯情况是什么原因?急谢谢原因如下:手机银行软件版本有误,建议去农行官网下载个最新版本。手机网络不行,建议换个工具登录试试。手机上网方式设置错误,建议改
JAVA一个简单的两个线程例子 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 于Java好的方面
虚构险情类通讯网络诈骗,常见的类型包括()。A、虚构车祸B、虚构绑架C、虚构手术D、虚构包裹藏毒正确答案:ABCD