1、javascript 创建XMLHttpRequest 对象-var xmlHttp = false;if(window.ActiveXObject)xmlHttp=new ActiveXObject(Microsoft.XMLHTTP);else if(window.XMLHttpRequest)xmlHttp=new XMLHttpRequest(); 发送请求-function checkUser() var userName = document.getElementById (userName).value;var url = CheckUserName?userName= + us
2、erName;xmlHttp.open(GET, url, true);xmlHttp.onreadystatechange = updatePage;xmlHttp.send(null); 返回结果处理- function updatePage() if (xmlHttp.readyState = 4)if(xmlHttp.status=200)var response = xmlHttp.responseText;var result=此帐号可以使用! ;if(response.indexOf(true)! =-1) result=很遗憾,该帐号已经被占用,请您另选一个document.g
3、etElementById(result).innerHTML=result;/script/headbodybrform id=regForm action= table border=0 align=center width=400tbodytrth colspan=2/th/trtr class=zi9pttd bgcolor=right帐号:/tdtd input type = text value = id = name= onblur=checkUser()/td colspan =2 div align = style=12px;color=red提示:用户帐号,不能包含空格、t
4、d align=密码:tdinput type=password value= id=pswd确认密码:repswdtd colspan=2 align=centersubmitvalue= 确&定&/tbody/table/form/body/htmlServlet 类CheckUserName.javapackage ;import java.io.IOException;import java.io.PrintWriter;import java.sql.DriverManager;import java.sql.SQLException;import javax.servle
5、t.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.sql.*;public class CheckUserName extends HttpServlet public CheckUserName() super();public void destroy() super.destroy();public void doGet(
6、HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException response.setContentType(text/htmlPrintWriter out = response.getWriter();/接收数据String userName=request.getParameter(/数据库链接tryClass.forName(com.microsoft.jdbc.sqlserver.SQLServerDrivercatch(ClassNotFoundExcepti
7、on e1)e1.printStackTrace();String jdbcURL=jdbc:microsoft:sqlserver:/localhost:1433;DatabaseName=newsConnection conn = null;try conn = DriverManager.getConnection(jdbcURL,sa,lcatch(SQLException e)e.printStackTrace();PreparedStatement pst;trypst = conn.prepareStatement (select * from admin where userN
8、ame=? pst.setString(1,userName);ResultSet rs=pst.executeQuery();boolean isExist=false;if(rs.next()isExist=true; out.println(isExist);catch(SQLException e) out.close();public void doPost(HttpServletRequest request,HttpServletResponse response)public void init() throws ServletException web.xml 配置文件中Se
9、rvlet 说明servletservlet-nameCheckUserNameservlet-class.CheckUserName/servletservlet_mappingurl-pattern/CheckUserName/servlet-mapping2、jsp在线用户显示与数目统计(没有连接数据库):Login.jsp 页面(登陆页面) import=java.sql.* errorPage=DOCTYPE html PUBLIC -/W3C/DTD XHTML 1.0 Transitional/ENhttp:/www.w3.org/TR/xhtml1/DTD/xhtml1-tra
10、nsitional.dtdhtml xmlns=/www.w3.org/1999/xhtmlmeta http-equiv=Content-Type content= /用户登录form1 name= method=postlogin_deal.jsp 用户名: 密&码:pwdSubmit提交resetSubmit2重置login_deal.jsp页面(登陆处理页面,设置的登录用户名和密码要求一样)处理结果Ok.jsp页面(核心代码:统计用户数并进行显示)% page import=java.util.*String username=(String)session.getAttribute(
11、int p=0;ArrayList list;list = (ArrayList)application.getAttribute(userlistif ( list=null ) list = new ArrayList(); list.add(username);else if ( list.indexOf(username)=-1 ) application.setAttribute(, list);ArrayList ollist = (ArrayList)application.getAttribute(out.println(在线用户如下:for (int i=0; iollist
12、.size(); i+) out.println(ollist.get(i) + p+; out.println(在线用户数:+p);二、Jsp+servlet实现数学计算:1、input.Jsp页面(输入数据)计算输入界面 b= select name=oper size=1option value =+-option value=*3/计算iframe name = width=100%/iframe2、myservlet,java页面(完成计算)package com;import javax.servlet.ServletContext;import javax.servlet.htt
13、p.HttpSession;public class myservlet extends HttpServlet protected void doGet(HttpServletRequest request, HttpServletResponse response) doPost(request,response);protected void doPost(HttpServletRequest request, PrintWriter out=response.getWriter();String da = (String)request.getParameter(String db = (String)request.getParameter(String op = (String)request.getParameter(String str = 没有计算结果int a = Integer.parseInt(da);int b = Integer.parseInt(db);int c = 0; if (op.equals() c = a+b; str = a+b= + c; else if (op.equals( c = a-b;a-b= c = a*b;a*b= c = a/b;a/b= servlet实现的计算结果:+str);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1