简单搜索

-----------页面
<%@ page language="java" contentType="text/html; charset=gbk"
    pageEncoding="gbk"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>管理菜单</title>
<style type="text/css">
a{
color:blue;
text-decoration: none;
font-size: 16px;
}
a:hover{
color:red;
text-decoration: none;
font-size: 16px;
}
</style>

<script type="text/javascript">
function search(){
var username=document.getElementById("username").value;
if(username==""||username==null){
alert("请输入内容!");
return false
}else{
window.parent.frames['right'].location.href="/Sigma365/SearchUsersServlet.do?username="+username;
}

}
function menu(name){
if(name=='userlist'){
window.parent.frames["right"].location.href="/Sigma365/UserAdminServlet.do";
}
if(name=='adminCompetence'){
window.parent.frames["right"].location.href="/Sigma365/CompetenceServlet.do";
}

}
</script>

</head>
<body leftmargin="0" topmargin="0" rightmargin="0">
<form  method="post">
<table align="right" cellpadding="0" cellspacing="0" >
<tr>
<td align="center">
<input type="text" size="15" maxlength="15" name="username" id="username">
<input type="submit" onclick="search()" value="搜索用户">&nbsp;&nbsp;
</td>
<td align="center">
<a href="javascript:menu('userlist')">
<font style="font-size: 13px;">用户列表</font>
</a>&nbsp;&nbsp;
</td>
<td align="center">
<a href="javascript:menu('adminCompetence')">
<font style="font-size: 13px;">权限列表</font>
</a>&nbsp;&nbsp;
</td>
<td align="center">
<a href="/Sigma365/GetAllGroupServlet.do" target="right">
<font style="font-size: 13px;">客户组</font>
</a>
</td>
</tr>
</table>
</form>
</body>
</html>



---------逻辑代码
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("GBK");
response.setContentType("text/html");
this.doPost(request, response);
}

@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("GBK");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String username=request.getParameter("username");

UserAdminDaoImpl user =new UserAdminDaoImpl();
List list=user.searchUser(username);
if(list.size()>0){
System.out.println("searchok");
request.setAttribute("list", list);
request.setAttribute("size", list.size());
request.getRequestDispatcher("super/pages/SuperAdminUserSearch.jsp").forward(request, response);
}else if(list.size()<=0){
out.print("<script>alert('没有要搜索的数据!');location.href='/Sigma365/UserAdminServlet.do';</script>");
}else{
out.print("<script>alert('搜索失败!');location.href='/Sigma365/UserAdminServlet.do';</script>");
}
}


public List searchUser(String userName) {
List list = new ArrayList();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
conn = dbConnectin.getConnection();
String sql = "select *from users where username like ?";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, userName+"%");
rs = ps.executeQuery();
while (rs.next()) {
UserBean user = new UserBean();
ps.setString(1, userName);
user.setUid(rs.getInt("uid"));
user.setUsername(rs.getString("username"));
user.setName(rs.getString("name"));
user.setPhone(rs.getString("phone"));
user.setEmail(rs.getString("email"));
user.setAdds(rs.getString("adds"));
user.setCompany(rs.getString("company"));
user.setAccess_level(rs.getInt("access_level"));
list.add(user);
}
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}

你可能感兴趣的:(html,sql,jsp,Access)