人事管理系统:登陆

首先,考虑到这个项目的用途是管理公司的是人事信息的,那么就必须考虑这个系统的安全性。因此,实现这个系统的第一功能就是登陆界面。该功能的主要用途是验证登入系统的账户与密码是否匹配。

总的来说,我打算使用用户密码、验证码双重方式来验证。开发环境为:JSP+servlet+mysql

1.注册servlet,由于登录页面需要验证码的功能,所以使用两个servlet。注册信息如下:[cce_xml]
<servlet>
<servlet-name>com.kms.servlet.Sidservlet</servlet-name>
<servlet-class>com.kms.servlet.Sidservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>com.kms.servlet.Sidservlet</servlet-name>
<url-pattern>/sid.servlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>com.kms.servlet.LoginServlet</servlet-name>
<servlet-class>com.kms.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>com.kms.servlet.LoginServlet</servlet-name>
<url-pattern>/login.servlet</url-pattern>
</servlet-mapping>
[/cce_xml]
2.实现servlet服务,主要代码如下:

LoginServlet.java[cce_cpp]
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Login person=new Login();
req.setCharacterEncoding(“UTF-8”);
resp.setCharacterEncoding(“UTF-8”);
PersonDao personDao=new PersonDao();
String sid=req.getParameter(“sid”);
String s=(String)req.getSession().getAttribute(“sid”);
if(!sid.equals(s)){
PrintWriter out=resp.getWriter();
String js=”<script>alert(‘Identifying code error !’);window.history.back(-1)</script>” ;
out.write(js);
}
else
{
person=personDao.news(req.getParameter(“l_name”),req.getParameter(“l_password”));
if(person.getL_id()!=0){
//创建session
HttpSession session=req.getSession();
session.setAttribute(“person”, person);
resp.sendRedirect(“admin/index.jsp”);
}
else{
PrintWriter out=resp.getWriter();
String js=”<script>alert(‘username or password error !’);window.history.back(-1)</script>” ;
out.write(js);
}
}
}
}
[/cce_cpp]

Sidservlet.java[cce_cpp]
public class Sidservlet extends HttpServlet{
private final static char[] cs=new char[]{‘0′,’1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’,
‘9’,’a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’, ‘k’, ‘l’, ‘m’,
‘n’, ‘o’, ‘p’, ‘q’, ‘r’, ‘s’, ‘t’, ‘u’, ‘v’, ‘w’, ‘x’, ‘y’, ‘z’,
‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’, ‘I’, ‘J’, ‘K’, ‘L’, ‘M’,
‘N’, ‘O’, ‘P’, ‘Q’, ‘R’, ‘S’, ‘T’, ‘U’, ‘V’, ‘W’, ‘X’, ‘Y’, ‘Z’};
private final static Random RANDOM=new Random();
private String sid() {
String string=””;
StringBuffer sbBuffer=new StringBuffer();
for(int i=0;i<4;i++)
{
sbBuffer.append(cs[RANDOM.nextInt(cs.length)]);
}
string=sbBuffer.toString();
return string;
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String sid =sid();
BufferedImage bi=new BufferedImage(50, 22, BufferedImage.TYPE_3BYTE_BGR);
Graphics g =bi.getGraphics();
g.setColor(Color.pink);
g.fillRect(0, 0, 50, 22);
g.setFont(new Font(“黑体”, Font.PLAIN, 22 ));
Random random = new Random();
//干扰素
g.setColor(Color.BLUE);
for(int i = 0; i < 10; i++) {
int x = random.nextInt(50);
int y = random.nextInt(22);
int x2 = random.nextInt(50);
int y2 = random.nextInt(22);
g.drawLine(x, y, x + x2, y + y2);
}
g.setColor(Color.BLACK);
g.drawString(sid, 0, 20);
req.getSession().setAttribute(“sid”,sid);
ImageIO.write(bi, “JPG”, resp.getOutputStream());
}
}
[/cce_cpp]

3.写出相应的jsp登录页面

login.jsp[cce_html]
<h1><strong>人事管理系统</strong> </h1>
<form action=”login.servlet” method=”post”>
<input type=”text” name=”l_name” value=”请输入用户名” onfocus=”javascript:if(this.value==’请输入用户名’)this.value=”;this.type=’text'”/>
<input type=”upassword” name=”l_password” value=”请输入密码” onfocus=”javascript:if(this.value==’请输入密码’)this.value=”;this.type=’password'”/>
<input type=”text” name=”sid” value=”请输入验证码” onfocus=”javascript:if(this.value==’请输入验证码’)this.value=”;this.type=’text'”/><img alt=”没有验证码,无法继续” src=”sid.servlet” id=”sid”>
<a href=”javascript:reloading()”>看不清</a>
<input type=”submit” class=”blue”value=”登录”>
</form>

<script>function reloading(){
var img=document.getElementById(“sid”);
img.src=”sid.servlet?abc=”+Math.random();
}
</script>
[/cce_html]

4.最终效果


You may also like...

发表评论

电子邮件地址不会被公开。 必填项已用*标注