上一篇介绍了JAVA_WEB项目之新闻发布管理系统第一篇:前期环境准备,接下来是实现该系统的登陆和注销的功能。
下面就直接给出登陆界面的form表单提交的内容:
%String root=request.getContextPath(); %>
<FORM id="form1" name="form1" onsubmit="javascript:return WebForm_OnSubmit();"
action="<%=root%>/UserAction?action=Login" method="post">
<SCRIPT type=text/javascript>
</SCRIPT>
<SCRIPT src="login_files/WebResource.axd" type=text/javascript></SCRIPT>
<SCRIPT src="login_files/WebResource(1).axd" type=text/javascript></SCRIPT>
<SCRIPT src="login_files/ScriptResource.axd" type=text/javascript></SCRIPT>
<SCRIPT src="login_files/ScriptResource(1).axd" type=text/javascript></SCRIPT>
<SCRIPT type=text/javascript>
function WebForm_OnSubmit() {
if (typeof(ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false) return false;
return true;
}
</SCRIPT>
<SCRIPT type=text/javascript>
</SCRIPT>
<DIV id=UpdatePanel1>
<DIV id=div1
style="LEFT: 0px; POSITION: absolute; TOP: 0px; BACKGROUND-COLOR: #0066ff"></DIV>
<DIV id=div2
style="LEFT: 0px; POSITION: absolute; TOP: 0px; BACKGROUND-COLOR: #0066ff"></DIV>
<SCRIPT language=JavaScript>
var speed=20;
var temp=new Array();
var clipright=document.body.clientWidth/2,clipleft=0
for (i=1;i<=2;i++){
temp[i]=eval("document.all.div"+i+".style");
temp[i].width=document.body.clientWidth/2;
temp[i].height=document.body.clientHeight;
temp[i].left=(i-1)*parseInt(temp[i].width);
}
function openit(){
clipright-=speed;
temp[1].clip="rect(0 "+clipright+" auto 0)";
clipleft+=speed;
temp[2].clip="rect(0 auto auto "+clipleft+")";
if (clipright<=0)
clearInterval(tim);
}
tim=setInterval("openit()",100);
</SCRIPT>
<DIV> </DIV>
<DIV>
<TABLE cellSpacing=0 cellPadding=0 width=900 align=center border=0>
<TBODY>
<TR>
<TD style="HEIGHT: 105px"><IMG src="login_files/login_1.gif"
border=0></TD></TR>
<TR>
<TD background=login_files/login_2.jpg height=300>
<TABLE height=300 cellPadding=0 width=900 border=0>
<TBODY>
<TR>
<TD colSpan=2 height=35></TD></TR>
<TR>
<TD width=360></TD>
<TD>
<TABLE cellSpacing=0 cellPadding=2 border=0>
<TBODY>
<TR>
<TD style="HEIGHT: 28px" width=80>登 录 名:</TD>
<TD style="HEIGHT: 28px" width=150><INPUT id=txtName
style="WIDTH: 130px" name=username></TD>
<TD style="HEIGHT: 28px" width=370><SPAN
id=RequiredFieldValidator3
style="FONT-WEIGHT: bold; VISIBILITY: hidden; COLOR: white">请输入登录名</SPAN></TD></TR>
<TR>
<TD style="HEIGHT: 28px">登录密码:</TD>
<TD style="HEIGHT: 28px"><INPUT id=txtPwd style="WIDTH: 130px"
type=password name=password></TD>
<TD style="HEIGHT: 28px"><SPAN id=RequiredFieldValidator4
style="FONT-WEIGHT: bold; VISIBILITY: hidden; COLOR: white">请输入密码</SPAN></TD></TR>
<TD style="HEIGHT: 18px"></TD>
<TD style="HEIGHT: 18px"></TD>
<TD style="HEIGHT: 18px"></TD></TR>
<TR>
<TD></TD>
<TD><INPUT id=btn
style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px"
onclick='javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("btn", "", true, "", "", false, false))'
type=image src="login_files/login_button.gif" name=btn>
</TD></TR>
<td></td><td>${message}</td>
</TBODY></TABLE></TD></TR></TBODY></TABLE></TD></TR>
<TD><IMG src="login_files/login_3.jpg"
border=0></TD></TR>
</TBODY></TABLE></DIV></DIV>
<SCRIPT type=text/javascript>
<!--
var Page_Validators = new Array(document.getElementById("RequiredFieldValidator3"), document.getElementById("RequiredFieldValidator4"));
// -->
</SCRIPT>
表单提交到后台的查看登陆名和密码的相关代码:
首先贴出解决全局网站编码的过滤
实体User:
package com.news.domain;
public class User {
private Integer user_id;
private String username;
private String password;
public Integer getUser_id() {
return user_id;
}
public void setUser_id(Integer user_id) {
this.user_id = user_id;
}
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 User(Integer user_id, String username, String password) {
super();
this.user_id = user_id;
this.username = username;
this.password = password;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
}
Controlle层:
package com.news.action;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.news.domain.User;
import com.news.service.UserService;
public class UserAction extends BaseAction{
private UserService service=new UserService();
public void Login(HttpServletRequest request,HttpServletResponse response){
service.findUser(request, response);
}
public void Exit(HttpServletRequest request,HttpServletResponse response){
try {
HttpSession session= request.getSession(false);
User user= (User) session.getAttribute("user");
if(user!=null){
session.removeAttribute("user");
}
response.getWriter().write("<script>window.parent.location.href='"+request.getContextPath() +"/login.jsp"+"';</script>");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.news.service;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.news.dao.UserDao;import com.news.domain.User;public class UserService {private UserDao userDao=new UserDao();public void findUser(HttpServletRequest request,HttpServletResponse response){User user= new User();user.setUsername(request.getParameter("username"));user.setPassword(request.getParameter("password"));System.out.println(user.getPassword()+","+user.getUsername());User uu = null;try{if(user!=null){uu=userDao.find(user);if(uu==null){request.setAttribute("message", "用户名或密码错误");request.getRequestDispatcher("/login.jsp").forward(request, response);return;} request.getSession().setAttribute("user", uu); request.getRequestDispatcher("/WEB-INF/jsps/index.jsp").forward(request, response);}} catch (Exception e) { throw new RuntimeException(e);}}}service层:
package com.news.service;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.news.dao.UserDao;
import com.news.domain.User;
public class UserService {
private UserDao userDao=new UserDao();
public void findUser(HttpServletRequest request,HttpServletResponse response){
User user= new User();
user.setUsername(request.getParameter("username"));
user.setPassword(request.getParameter("password"));
System.out.println(user.getPassword()+","+user.getUsername());
User uu = null;
try{
if(user!=null){
uu=userDao.find(user);
if(uu==null)
{
request.setAttribute("message", "用户名或密码错误");
request.getRequestDispatcher("/login.jsp").forward(request, response);
return;
}
request.getSession().setAttribute("user", uu);
request.getRequestDispatcher("/WEB-INF/jsps/index.jsp").forward(request, response);
}
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
}
dao层:
package com.news.dao;
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import com.news.domain.User;
import com.news.utils.JdbcUtils;
public class UserDao {
public User find(User user)
{
Connection conn=null;
try {
conn=JdbcUtils.getConnection();
QueryRunner runner=new QueryRunner();
String sql="select * from tb_user where username=? and password=?";
Object[] params={user.getUsername(),user.getPassword()};
User u = (User) runner.query(conn, sql, params, new BeanHandler(User.class));
return u;
} catch (SQLException e) {
throw new RuntimeException(e);
}finally{
DbUtils.closeQuietly(conn);
}
}
}
登陆功能和注销的主要代码就在上面,接下来实现 新闻发布,新闻修改,新闻删除功能的实现。整个系统的源码会在最后一篇介绍的博文中贴出下载链接。