粘贴自己用

djk1990 2011-11-13 11:10:28
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();
}
}
}
...全文
96 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
longyi008 2011-11-17
  • 打赏
  • 举报
回复
粘贴自己用
z_f_p 2011-11-17
  • 打赏
  • 举报
回复
自己用!
随风醉舞 2011-11-14
  • 打赏
  • 举报
回复
啥情况!!
djk1990 2011-11-13
  • 打赏
  • 举报
回复
@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();
}
{

}

}

}
djk1990 2011-11-13
  • 打赏
  • 举报
回复
public static String filterNull(String str)
{
if(str == null)
return "";
return str;
}
djk1990 2011-11-13
  • 打赏
  • 举报
回复
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;
}

23,407

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧