java+jsp聊天室

java+jsp聊天室,第1张

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver")

String url="jdbc:microsoft:sqlserver://localhost:1433DatabaseName=Northwind"

String user="sa"

String password=""

Connection conn=DriverManager.getConnection(url,user,password)

Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE)

String sql="select * from Customers"

ResultSet rs=stmt.executeQuery(sql)

看看这段代码,就是连接数据库的代码

用户验证的话就在写一段

先半INPUT里面的值传到验证的页面,把这个值跟数据库的值进行对比,就可以了撒!

这是我写的验证代码

<%@ page import="weaver.general.Util" %>

<%@ page import="java.util.*" %>

<%@ page import="java.sql.Timestamp" %>

<jsp:useBean id="RecordSet" class="weaver.conn.RecordSet" scope="page" />

<jsp:useBean id="YcVerifyLogin" class="weaver.login.YcVerifyLogin" scope="page" />

<%

Date newdate = new Date()

long datetime = newdate.getTime()

Timestamp timestamp = new Timestamp(datetime)

String CurrentDate = (timestamp.toString()).substring(0,4) + "-" + (timestamp.toString()).substring(5,7) + "-" +(timestamp.toString()).substring(8,10)

String CurrentTime = (timestamp.toString()).substring(11,13) + ":" + (timestamp.toString()).substring(14,16) + ":" +(timestamp.toString()).substring(17,19)

String loginfile = Util.null2String(request.getParameter("loginfile"))

String logintype = Util.null2String(request.getParameter("logintype"))

String loginid = Util.null2String(request.getParameter("loginid"))

String logincode = Util.null2String(request.getParameter("logincode")) //验证码

String userpassword = Util.null2String(request.getParameter("userpassword"))

if(logintype.equals("")) logintype="1"

if(loginfile.equals("")) loginfile="/login/Login.jsp?logintype="+logintype

String RedirectFile = "/main.jsp"

if (logintype.equals("2")) RedirectFile = "/portal/main.jsp"

String sql=""

if(!logincode.equals(Util.getEncrypt(loginid+CurrentDate))){

response.sendRedirect(loginfile)

}

if(!loginid.equals("")){

sql="select * from ycuser where loginid='"+loginid+"' and logintype='"+logintype+"'"

RecordSet.executeSql(sql)

if(RecordSet.next()){

loginid=RecordSet.getString(1)

String usercheck = YcVerifyLogin.getUserCheck(request,response,loginid,userpassword,logintype,loginfile)

if(usercheck.equals("15") || usercheck.equals("16") || usercheck.equals("17")) response.sendRedirect(loginfile + "&message="+usercheck)

else if(usercheck.equals("19")){

response.sendRedirect("/system/InLicense.jsp?message=0")

}

else {

response.sendRedirect(RedirectFile+"?logmessage="+usercheck)

}

}else{

response.sendRedirect(loginfile)

}

}else{

response.sendRedirect(loginfile)

}

%>

希望对你有帮助!

JSP页面:

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>

<%

String path = request.getContextPath()

String basePath = request.getScheme() + "://"

+ request.getServerName() + ":" + request.getServerPort()

+ path + "/"

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

</head>

<body>

<form action="DoApplication" method="post">

输入聊天信息:

<br />

<textarea name="talk" rows="5" cols="40"></textarea>

<br />

<input type="submit" value="确认">

<input type="reset" value="清空">

</form>

<hr />

已有的聊天信息:

<br />

<%

List app = null

app = (List) application.getAttribute("app")

if (app != null) {

%>

<textarea rows="10" cols="40" style="text-align: left">

<%

Iterator ite = app.iterator()

while (ite.hasNext()) {

String info = (String) ite.next()

%>

<%=info%>

<%

}

}

%>

</textarea>

</body>

</html>

------------------------------------------------

Severlet:

public class DoApplication extends HttpServlet {

/**

* Constructor of the object.

*/

public DoApplication() {

super()

}

/**

* Destruction of the servlet. <br>

*/

public void destroy() {

super.destroy()// Just puts "destroy" string in log

// Put your code here

}

/**

* The doGet method of the servlet. <br>

*

* This method is called when a form has its tag value method equals to get.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

String info=(String)request.getParameter("talk")

ServletContext application = this.getServletContext()

List appList=null

appList=(List)application.getAttribute("app")

if(appList!=null){

appList.add(info)

}else{

appList=new ArrayList()

appList.add(info)

}

application.setAttribute("app", appList)

response.setCharacterEncoding("gbk")

request.getRequestDispatcher("index.jsp").forward(request, response)

}

/**

* The doPost method of the servlet. <br>

*

* This method is called when a form has its tag value method equals to post.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

this.doGet(request, response)

}

/**

* Initialization of the servlet. <br>

*

* @throws ServletException if an error occurs

*/

public void init() throws ServletException {

// Put your code here

}

}

应该不用写进数据库了

直接存到session里面就可以了

登录日期只是针对本次会话的

如果每次登陆都更新数据库数据

得给数据库多大压力

如果我疯狂刷新页面,数据库不就完蛋了

如果时间字段对下次有用的话,就得存入数据库了

session *** 作 楼主应该会的吧


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/10772414.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-11
下一篇2023-05-11

发表评论

登录后才能评论

评论列表(0条)

    保存