我是靠谱客的博主 动听老师,最近开发中收集的这篇文章主要介绍java完整的用户登录系统_J2EE开发案例:用户登录系统,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

一、需求

检查用户的合法性实现及安全登录。

新用户的注册

用户密码遗忘后通过注册邮箱来重新获取

二、设计

由JSP实现表示,其中包括登录页面、注册页面等。

由servlet实现转发控制,处理用户登录请求、注册请求以及取回消息请求。

由CMP表示用户注册信息,实现用户的增加及查找等操作。

由MDB实现包含密码信息邮件的发送。

三、实现时注意的问题:

1、下载argosoft mail server,安装启动时发现异常,原因是25号端口被XP系统的SMTP服务占用,将该服务停止,重新启动邮件服务器正常。

2、在servlet的各方法中,需要将response对象的编码格式设置为gb2312,否则forward后汉字无法正常显示,出现乱码。代码如下:

/*

* Created on 2004-6-8

*

* To change the template for this generated file go to

* Window – Preferences – Java – Code Generation – Code and Comments

*/

package com.study.exercise;

import java.io.IOException;

import java.io.PrintWriter;

import javax.ejb.CreateException;

import javax.ejb.FinderException;

import javax.naming.InitialContext;

import javax.servlet.RequestDispatcher;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.naming.*;

import javax.jms.*;

/**

* @author Administrator

*

* To change the template for this generated type comment go to

* Window – Preferences – Java – Code Generation – Code and Comments

*/

public class ProcessServlet extends HttpServlet {

protected void doGet(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

//TODO Method stub generated by Lomboz

doProcess(request,response);

}

protected void doPost(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

//TODO Method stub generated by Lomboz

doProcess(request,response);

}

protected void doProcess(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

//TODO Method stub generated by Lomboz

String operation=request.getParameter(“operation”);

if(operation.equals(“logon”))doLogon(request,response);

if(operation.equals(“reg”))doReg(request,response);

if(operation.equals(“retake”))doRetake(request,response);

}

protected void doLogon(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

response.setCharacterEncoding(“gb2312”);

String id=request.getParameter(“userID”);

//  将用户ID放入会话

request.getSession().setAttribute(“userid”,id);

String password=request.getParameter(“password1”);

String resultpage=”/index.jsp”;

PrintWriter out =response.getWriter();

try{

InitialContext ctx=new InitialContext();

Object objRef = ctx.lookup(“UserBean”);

//   主接口

UserHome

home=(UserHome)javax.rmi.PortableRemoteObject.narrow(

objRef,UserHome.class);

User bean =home.findByPrimaryKey(id);

String ps=bean.getPassword();

if(!ps.equals(password))resultpage=”/notFindUser.jsp”;

}catch (FinderException e){

resultpage=”/notFindUser.jsp”;

}

catch(javax.naming.NamingException ne){

out.println(“Naming Exception caught:”+ne);

ne.printStackTrace(out);

}catch(java.rmi.RemoteException re){

out.println(“Remote Exception caught:”+re);

re.printStackTrace(out);

}finally{

RequestDispatcher dispatcher =

getServletConfig().getServletContext().getRequestDispatcher(resultpage);

dispatcher.forward(request, response);

}

}

protected void doReg(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

response.setCharacterEncoding(“gb2312”);

String resultpage=”/regsucess.jsp”;

String id=request.getParameter(“userID”);

String password=request.getParameter(“password”);

String password1=request.getParameter(“password1”);

String email_address=request.getParameter(“email_address”);

if(!password.equals(password1)){

request.setAttribute(“promote”,”两次密码不一致!”);

resultpage=”/reg.jsp”;

RequestDispatcher dispatcher =

getServletConfig().getServletContext().getRequestDispatcher(resultpage);

dispatcher.forward(request, response);

}else{

PrintWriter out =response.getWriter();

try{

InitialContext ctx=new InitialContext();

Object objRef = ctx.lookup(“UserBean”);

//    主接口

UserHome

home=(UserHome)javax.rmi.PortableRemoteObject.narrow(

objRef,UserHome.class);

// bean=home.findByPrimaryKey(id);

//    组件接口

//Integer i=new Integer(1);

int i=1;

User bean =home.create(id,password,i,email_address);

resultpage=”/regsucess.jsp”;

}

catch (CreateException e){

request.setAttribute(“promote”,”帐户已经存在,请选择其他帐户名称!”);

resultpage=”/reg.jsp”;

}

catch(javax.naming.NamingException ne){

out.println(“Naming Exception caught:”+ne);

ne.printStackTrace(out);

}catch(java.rmi.RemoteException re){

out.println(“Remote Exception caught:”+re);

re.printStackTrace(out);

}finally{

RequestDispatcher dispatcher =

getServletConfig().getServletContext().getRequestDispatcher(resultpage);

dispatcher.forward(request, response);

}

}

}

protected void doRetake(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

//发送消息给队列

response.setCharacterEncoding(“gb2312”);

PrintWriter out =response.getWriter();

String userid=(String)request.getSession().getAttribute(“userid”);

try {

// Get access to JNDI

Context context = new InitialContext();

// Lookup the managed connection factory for a topic

TopicConnectionFactory topicFactory =

(TopicConnectionFactory)context.lookup(“ConnectionFactory”);

// Create a connection to the JMS provider

TopicConnection topicConnection = topicFactory.createTopicConnection();

// Creat a topic session

TopicSession session = topicConnection.createTopicSession(

// No transaction

false,

// Auto ack

Session.AUTO_ACKNOWLEDGE);

// Lookup the destination you want to publish to

Topic topic = (Topic)context.lookup(“topic/logonTopic”);

// Create a publisher

TopicPublisher pub = session.createPublisher(topic);

// Create a message

TextMessage message = session.createTextMessage();

//将用户ID 发送给消息驱动EJB

message.setText(userid);

// Publish the message

pub.publish(topic, message);

// Close the stuff

session.close();

topicConnection.close();

}

catch (Exception e) {

e.printStackTrace();

}

finally{

RequestDispatcher dispatcher =

getServletConfig().getServletContext().getRequestDispatcher(“/index.jsp”);

dispatcher.forward(request, response);

}

}

}

3、在实现CMP时,在META-INF目录下需要增加jbossdmp-jdbc.xml文件,实现数据源的设置以及属性与表字段的映射。配置如下:

-//JBoss//DTD JBOSSCMP-JDBC 3.0//EN” “http://www.jboss.org/j2ee/dtd/jbosscmp-jdbc_3_0.dtd“>

java:/MySqlDS

mySQL

true

false

false

true

false

foreign-key

on-load

1000

*

1000

To add beans that you have deployment descriptor info for, add

a file to your XDoclet merge directory called jbosscmp-jdbc-beans.xml

that contains the markup for those beans.

–>

User

userinfo

userID

userID

password

password

level

level

email_address

email_address

同样,在该目录下需要增加jboss.xml文件,对EJB进行配置。配置如下:

-//JBoss//DTD JBOSS 3.0//EN” “http://www.jboss.org/j2ee/dtd/jboss_3_0.dtd“>

nobody

To add beans that you have deployment descriptor info for, add

a file to your XDoclet merge directory called jboss-beans.xml that contains

the , and

markup for those beans.

–>

User

UserBean

UserLocal

SendPassword

topic/logonTopic

另外,在jboss/default/deploy目录下,需要增加mysql-ds.xml文件,实现数据源的配置。配置如下:

http://www.mysql.com/downloads/api-jdbc-stable.html

–>

MySqlDS

jdbc:mysql://localhost:3306/test

org.gjt.mm.mysql.Driver

root

org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter

some arbitrary sql

–>

some arbitrary sql

–>

mySQL

4、在deploy/jms目录中,jbossmq-destinations-service.xml文件中需要新增logonTopic。配置如下:

| This file defines the default Queues and Topics that JBossMQ

| ships with.  The default Queues and Topics are used by the

| JBoss test suite and by the sample jms programs.

|

| You can add other destinations to this file, or you can create other

| *-service.xml files to contain your application’s destinations.

–>

a SecurityConf will default to role guest with read=true, write=true,

create=false.

–>

name=”jboss.mq.destination:service=Topic,name=testTopic”>

jboss.mq:service=DestinationManager

jboss.mq:service=SecurityManager

name=”jboss.mq.destination:service=Topic,name=logonTopic”>

jboss.mq:service=DestinationManager

jboss.mq:service=SecurityManager

name=”jboss.mq.destination:service=Topic,name=securedTopic”>

jboss.mq:service=DestinationManager

jboss.mq:service=SecurityManager

name=”jboss.mq.destination:service=Topic,name=testDurableTopic”>

jboss.mq:service=DestinationManager

jboss.mq:service=SecurityManager

name=”jboss.mq.destination:service=Queue,name=testQueue”>

jboss.mq:service=DestinationManager

jboss.mq:service=SecurityManager

-1

name=”jboss.mq.destination:service=Queue,name=A”>

jboss.mq:service=DestinationManager

name=”jboss.mq.destination:service=Queue,name=B”>

jboss.mq:service=DestinationManager

name=”jboss.mq.destination:service=Queue,name=C”>

jboss.mq:service=DestinationManager

name=”jboss.mq.destination:service=Queue,name=D”>

jboss.mq:service=DestinationManager

name=”jboss.mq.destination:service=Queue,name=ex”>

jboss.mq:service=DestinationManager

5、将整个应用打包为ear文件,部署到jboss。启动服务器应用正常运行。

最后

以上就是动听老师为你收集整理的java完整的用户登录系统_J2EE开发案例:用户登录系统的全部内容,希望文章能够帮你解决java完整的用户登录系统_J2EE开发案例:用户登录系统所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(52)

评论列表共有 0 条评论

立即
投稿
返回
顶部