粘贴自己用
public class DBUtil {/**
* 通过连接池获取数据库的链接
* @return
*/
public static Connection getConnection()
{
try {
Context context=new InitialContext();
DataSource ds=(DataSource)context.lookup("java:/comp/env/guestbook");
Connection conn=ds.getConnection();
return conn;
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 关闭数据库的链接
* @param conn
* @param st
* @param rs
*/
public void close(Connection conn,Statement st,ResultSet rs)
{
try {
if(rs!=null)
{
rs.close();
}
if(st!=null)
{
st.close();
}
if(rs!=null)
{
rs.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
--------------------编程问答-------------------- public static String filterHtml(String html)
{
if(html == null || html.length() == 0)
return "";
html = html.replaceAll("&", "&");
html = html.replaceAll("<", "<");
html = html.replaceAll(">", ">");
html = html.replaceAll(" ", " ");
html = html.replaceAll("'", "'");
html = html.replaceAll("\"", """);
html = html.replaceAll("\n", "<br/>");
return html;
} --------------------编程问答-------------------- public static String filterNull(String str)
{
if(str == null)
return "";
return str;
} --------------------编程问答-------------------- @WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 处理登录请求
request.setCharacterEncoding("utf-8");
String username=request.getParameter("username");
String password=request.getParameter("userpwd");
//判断用户名是否有效,无效则重新登录
if(username==null||username.trim().length()==0)
{
response.sendRedirect("login.html");
}
else
{//用户名有效,则当前用户添加到在线人员列表中
HttpSession session=request.getSession();
//判断当前用户是否已经登录
User user=(User)session.getAttribute("user");
if(user==null)
{
//当前未登录
user=new User();
user.setName(username);
session.setAttribute("user", user);//容器通知UserList类调用add方法
}
//显示欢迎信息
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("欢迎您," + username + " <a href='logout'>注销</a><br/>");
//列出当前在线人数和列表
UserList ul=UserList.getInstance();
out.println("<br/>当前在线人数:"+ul.getUserCount());
out.println("<br/>当前在线用户列表:");
Iterator< String>namelist=ul.getNameList();
int i=0;
while(namelist.hasNext())
{
out.println(namelist.next());
out.println(" nbsp;nbsp;nbsp;");
i++;
if(i%5==0)
{
out.println("<br/>");
}
}
out.flush();
out.close();
}
{
}
}
}
--------------------编程问答-------------------- 啥情况!! --------------------编程问答-------------------- 粘贴自己用 --------------------编程问答-------------------- 自己用!
补充:Java , 非技术区