熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

一個簡單JAVA網絡通訊錄

2013-11-23 19:26:12  來源: Java核心技術 

  定義一個簡單的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>


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26867.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.