- servlet只实例化一次
- 单例模型
- 全局变量的值不确定,线程不安全====避免声明全局变量
- 第一次:构造块 构造函数 init() service() super.service() doGet()/doPost()
第二次:super.service() doGet()/doPost()
123456 456 @WebServlet("/Life.action") public class LifeServlet extends HttpServlet { //servlet只实例化一次,单例模型 全局变量的值不确定,线程不安全====避免声明全局变量 { //实例块,在构造方法之前调用 System.out.println("1.构造代码块"); } public LifeServlet(){ System.out.println("2.构造方法"); } @Override public void init() throws ServletException { System.out.println("3.初始化init()"); } @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("4.service()"); super.service(req, resp);//5.判断请求方法是post还是get //get====>doGet //post===>doPost } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("6.doGet"); } @Override public void destroy() { //关闭容器时 System.out.println("7.servlet销毁"); super.destroy(); } }HTML-JAVA-数据库
1.html====>关键active 2.Servlet====> 关键1:@WebServlet("/reg.action")====>连接HTML 关键2:request.setCharacterEncoding("utf-8")====>解决参数编码问题 关键3:String uname = request.getParameter("uname")====>接收html中的数据 uname:标签中name属性的命名 关键4:中间连接数据库部分参考之前写过的文章JDBC部分 关键5:在HTML中显示(添加补充框架) response.setContentType("text/html;charset=utf-8");//设置编码 PrintWriter out = response.getWriter(); out.println(""); out.println("1111 "); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.close(); package ...; import ...; @WebServlet("/reg.action") public class RegServlet extends HttpServlet { @Override //提示这个方法是重写父类的 写错则会提示 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response);//当请求为post时请求也重写定位到get } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8");//解决参数编码问题 //接收参数 String uname = request.getParameter("uname"); String pwd = request.getParameter("pwd"); System.out.println(uname+" "+pwd); try { Class.forName("oracle.jdbc.driver.OracleDriver"); }catch (Exception ex){ ex.printStackTrace(); } int result=-1; try ( Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","a"); PreparedStatement pstmt = con.prepareStatement("insert into yc108 values(seq_yc108.nextval,?,?)"); ){//新写法 会自动关闭 pstmt.setString(1,uname); pstmt.setString(2,pwd); result = pstmt.executeUpdate(); }catch (Exception ex){ ex.printStackTrace(); } response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); if(result ==1){ out.println("添加"+uname+"成功"); }else if(result ==0){ out.println("添加"+uname+"失败"); }else{ out.println("系统异常,添加失败"); } out.close(); } }
封装代码汇总
调错思路看是哪一端的错误
- 客户端:在开发者工具
- 服务端:看自己的代码
- 获取session:客户端与服务器多次请求和响应的共享空间
HttpSession session = request.getSession(); session.getAttribute("userid"); session.setAttribute("userid",userid);2.ServletContext
ServletContext application = request.getSession().getServletContext(); application.getAttribute("onlineuserList"); application.setAttribute("onlineuserList",onlineuserList);3.显示本机ID+在线ID
private void number(HttpServletRequest request, HttpServletResponse response){ JsonModel jsonModel = new JsonModel(); int userid = 0; String ip = getIpAddress(request); String addr = ""; //获取session:客户端与服务器多次请求和响应的共享空间 HttpSession session = request.getSession(); if(session.getAttribute("userid")==null){ //如果id为空,则给一个随机数作为id userid = new Random().nextInt(1000); session.setAttribute("userid",userid); //获取application存这个用户编号====所有访问的用户编号 ServletContext application = request.getSession().getServletContext(); Map4.本ID退出onlineuserMap = new HashMap (); List
private void quit(HttpServletRequest request,HttpServletResponse response){ HttpSession session = request.getSession(); int id = Integer.parseInt(session.getAttribute("userid").toString()); //取出在线用户列表 ServletContext application = request.getServletContext(); List> onlineuserList = (List >) application.getAttribute("onlineuserList"); //在线用户列表中移出退出的id for (int i=0;i 5.保存聊天记录 private void sendMessage(HttpServletRequest request,HttpServletResponse response){ //取出在线用户列表 ServletContext application = request.getServletContext(); HttpSession session = request.getSession(); //取出用户id int userid = Integer.parseInt(session.getAttribute("userid").toString()); //取出该id说的话 String message = request.getParameter("message"); //获取现在的时间 Date date = new Date(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = simpleDateFormat.format(date); //历史聊天记录 ListmessageList = null; if (application.getAttribute("messageList")==null){ messageList = new ArrayList (); application.setAttribute("messageList",messageList); } messageList = (List ) application.getAttribute("messageList"); //拼接样式信息 StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(userid+"说:
"); stringBuffer.append(message+"
"); stringBuffer.append(""+time+""); messageList.add(stringBuffer.toString()); //System.out.println(messageList); JsonModel jsonModel = new JsonModel(); jsonModel.setCode(1); jsonModel.setData(messageList); try { super.writeJson(jsonModel,response);//json只有数组和对象,而list没有键,所以以数组形式保存 } catch (IOException e) { e.printStackTrace(); } }欢迎分享,转载请注明来源:内存溢出
评论列表(0条)