• 第一阶段冲刺


    登录与注册

    UserDaoilpl.java

    package utils;
    
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Set;
    import java.util.HashMap;  
    import java.util.Iterator;  
    import java.util.Map;
    
    import org.apache.http.client.config.RequestConfig;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.utils.URIBuilder;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Attributes;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;
    
    import dao.dao;
    import entity.Cvf;
    
    
    
    /**
     * 这是使用Jsoup解析
     */
    public class Jsouputil {
    
    	/**
    	 * 使用Selector选择器获取元素
    	 */
    	public static void testSelector()throws Exception{
    		//获取Document对象
    		HttpClientPool httpClientPool =new HttpClientPool();
    		//创建连接池管理器
    		PoolingHttpClientConnectionManager cm =new  PoolingHttpClientConnectionManager();
    		//获取网页HTML字符串
    		String content=httpClientPool.doGet(cm);
    						
    		//解析字符串
    		Document doc = Jsoup.parse(content);
    //		System.out.println(doc.toString());
    	
    		//[attr=value],利用属性获取
    		Elements elements = doc.select("div[id=content]").select("dl").select("dt[class=ptitle]");
    		System.out.println(elements.toString());
    		
    		Cvf cvf=new Cvf();
    		dao dao=new dao();
    		if(elements!=null) 
    		{
    		for(Element ele:elements)
    		{
    			String href="http://openaccess.thecvf.com/";
    			String cname=ele.select("a").text();
    			System.out.println(cname);
    			String href2=ele.select("a").attr("href");
    			String chref=href.concat(href2);
    			System.out.println(chref);
    			String cabstract =null;
    			String ckeyword  =null;
    				//获取title的内容
    			CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();
    				//创建URIBuilder
    				  URIBuilder uribuilder= new URIBuilder(chref);
    				  HttpGet httpGet= new HttpGet(uribuilder.build());
    				  RequestConfig config = RequestConfig.custom().setConnectTimeout(10000*10000)//设置创建连接的最长时间,单位为毫秒
    						  .setConnectionRequestTimeout(10000*10000)//设置获取连接的最长时间,单位为毫秒
    						  .setSocketTimeout(100000*1000000)//设置传输数据的最长时间,单位为毫秒
    						  .build();
    						  //给请求设置请求信息
    						  httpGet.setConfig(config);
    						  CloseableHttpResponse response=null;
    						  response = httpClient.execute(httpGet);
    		    	  //解析响应,获取数据
    		    	  //判断状态码是否为两百
    		    	  if(response.getStatusLine().getStatusCode()==200||response.getStatusLine().getStatusCode()==302) {
    		    		  Document document = Jsoup.parse(new URL(chref), 100000);
    				      cabstract = document.select("div[id=abstract]").text();
    				      System.out.println("已获取摘要");
    					String[] strs = strTostrArray(cname+cabstract);
    					  
    					 ckeyword=keyword(strs);
    					 
    		    	  }
    		    	  else {
    		    		  System.out.println(response.getStatusLine().getStatusCode());
    		    		  cabstract =null;
    		    		  ckeyword=null;
    		    	  }
    		    	  if(response!=null) {
    					  //关闭response 
    					  response.close();
    				  }
    		    	  
    			cvf=new Cvf(cname,chref,cabstract,ckeyword);
    			dao.add(cvf);
    
    		}
    		}
    		
    	}
    	public static String[] strTostrArray(String str) {
    		  /*
    		   * 将非字母字符全部替换为空格字符" " 得到一个全小写的纯字母字符串包含有空格字符
    		   */
    		  str = str.toLowerCase();// 将字符串中的英文部分的字符全部变为小写
    		  String regex = "[\W]+";// 非字母的正则表达式 --W:表示任意一个非单词字符
    		  str = str.replaceAll(regex, " ");
    		  String[] strs = str.split(" "); // 以空格作为分隔符获得字符串数组
    		  return strs;
    		 }
    		 public static String keyword(String[] strs) {
    		  /*
    		   * 建立字符串(String)出现次数(Integer)的映射
    		   */
    		  HashMap<String, Integer> strhash = new HashMap<String, Integer>();
    		  Integer in = null;// 用于存放put操作的返回值
    		  for (String s : strs) {// 遍历数组 strs
    
    				  in = strhash.put(s, 1);
    				  if (in != null) {// 判断如果返回的不是null,则+1再放进去就是出现的次数
    					  strhash.put(s, in + 1);
    				  }
    
    		  }
    		  Set<java.util.Map.Entry<String, Integer>> entrySet = strhash.entrySet();
    		  String maxStr = null;// 用于存放出现最多的单词
    		  int maxValue = 0;// 用于存放出现最多的次数
    		  for (java.util.Map.Entry<String, Integer> e : entrySet) {
    		   String key = e.getKey();
    		   Integer value = e.getValue();
    		   if(key.equals("a")||key.equals("the")||key.equals("to")||key.equals("and")||key.equals("in")||key.equals("of")||key.equals("our")||key.equals("your")||key.equals("we")||key.equals("is")||key.equals("on")||key.equals("for")||key.equals("that")||key.equals("an")||key.equals("are")) {
    			   value=0;
    		   }
    		   if (value > maxValue) {
    		    maxValue = value;// 这里有自动拆装箱
    		    maxStr = key;
    		   }
    		  }
    		  System.out.println("出现最多的单词是:" + maxStr + "出现了" + maxValue + "次");
    		  return maxStr;
    		 }
    }
    

      User.java

    package com.zzw.entity;
    
    public class User {
          private int uid;
          private String uname;
          private String upwd;
          private String type;
    	public int getUid() {
    		return uid;
    	}
    	public void setUid(int uid) {
    		this.uid = uid;
    	}
    	public String getUname() {
    		return uname;
    	}
    	public void setUname(String uname) {
    		this.uname = uname;
    	}
    	public String getUpwd() {
    		return upwd;
    	}
    	public void setUpwd(String upwd) {
    		this.upwd = upwd;
    	}
    	public String getType() {
    		return type;
    	}
    	public void setType(String type) {
    		this.type = type;
    	}
    	@Override
    	public String toString() {
    		return "User [uid=" + uid + ", uname=" + uname + ", upwd=" + upwd + ", type=" + type + "]";
    	}
    	public User() {
    		super();
    	}
    	public User(String uname, String upwd, String type) {
    		super();
    		this.uname = uname;
    		this.upwd = upwd;
    		this.type = type;
    	}
    	public User(int uid, String uname, String upwd, String type) {
    		super();
    		this.uid = uid;
    		this.uname = uname;
    		this.upwd = upwd;
    		this.type = type;
    	}
    	public User(String uname, String upwd) {
    		super();
    		this.uname = uname;
    		this.upwd = upwd;
    	}
          
    }
    

      UserServicelipl.java

    package com.zzw.service.Impl;
    
    
    
    
    import java.util.List;
    
    
    import com.zzw.dao.IUserDao;
    import com.zzw.dao.Impl.UserDaoImpl;
    import com.zzw.entity.User;
    import com.zzw.service.IUserService;
    
    public class UserServiceImpl implements IUserService{
    	IUserDao userdao= new UserDaoImpl();
    	//登录
    	 public User Login(User user) {
    
    	  	
    	  	return userdao.Login(user.getUname(),user.getUpwd());
    	 }
    	//注册
        public boolean Register(User user) {
      	  boolean flag=false;
      	  if(!userdao.isExist(user.getUname())) {
      		  userdao.Register(user);
      		  flag=true;
      	  }else {
      		  System.out.println("此人已存在");
      	  }
      	 return flag; 
        }
      //根据账号查询用户
        public User Query(String uname) {
      	  return userdao.Query(uname);
        }
        
    }
    

      LoginServlet.java

    package com.zzw.servlet;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import com.zzw.entity.User;
    import com.zzw.service.IUserService;
    import com.zzw.service.Impl.UserServiceImpl;
    
    
    public class LoginServlet extends HttpServlet {
    
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		request.setCharacterEncoding("utf-8");
    		response.setCharacterEncoding("utf-8");
    		response.setContentType("text/html; charset=utf-8");
    		String name= request.getParameter("uname");
    		String pwd= request.getParameter("upwd");
    		User user = new User(name,pwd);
    		//接口 x=new 实现类()
    		IUserService userservice = new UserServiceImpl();
    		User user2=userservice.Login(user);
    	    String rs=user2.getType();
    	    
    		if(rs.equals("1")) {
    			
    			request.setAttribute("message ","noerror");
    			request.getRequestDispatcher("index1.jsp").forward(request, response);
    		}else if(rs.equals("2"))
    		{
    			request.setAttribute("message","noerror");
    			request.getRequestDispatcher("index.jsp").forward(request, response);
    		}
    		else {
    			request.setAttribute("message","error");
    			request.getRequestDispatcher("login.jsp").forward(request, response);
    		}
    	}
    
    	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		doGet(request, response);
    	}
    
    }
    

      RegisterServlet.java

    package com.zzw.servlet;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import com.zzw.entity.User;
    import com.zzw.service.IUserService;
    import com.zzw.service.Impl.UserServiceImpl;
    
    
    
    
    public class RegisterServlet extends HttpServlet {
    
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		request.setCharacterEncoding("utf-8");
    		response.setCharacterEncoding("utf-8");
    		response.setContentType("text/html; charset=utf-8");
    		String name= request.getParameter("uname");
    		String pwd= request.getParameter("upwd");
    		String type= request.getParameter("type");
    		User user = new User(name,pwd,type);
    		//接口 x=new 实现类()
    		IUserService userservice = new UserServiceImpl();
    		boolean result=userservice.Register(user);
    	
    		if(!result) {
    			request.setAttribute("message","error");
    			request.getRequestDispatcher("register.jsp").forward(request, response);
    		}else {
    			request.setAttribute("message","noerror");
    			request.getRequestDispatcher("register.jsp").forward(request, response);
    		}
    	}
    
    	
    	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		doGet(request, response);
    	}
    
    }
    

      DBUtil.java

    package com.zzw.utils;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;
    
    
    public class DBUtil {
    	//数据库URL和账号密码
    	public static final String URL="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GB18030&useSSL=false&serverTimezone=GMT&allowPublicKeyRetrieval=true";//数据源  !!!!注意若出现加载或者连接数据库失败一般是这里出现问题
    	private static final String UNAME="root";
    			private static final String UPWD="1234";
    			public static PreparedStatement  pstmt=null;
    			public static ResultSet  rs = null;
    			public static Connection  connection=null;
    			
    			
         //增删改
    	 public static boolean executeUpdate(String sql,Object [] params) {
    		 boolean flag = false;
         	try {
         		
         	//a.导入驱动,加载具体的驱动类
         	Class.forName("com.mysql.cj.jdbc.Driver");
         	//b.与数据库建立连接
         	connection = DriverManager.getConnection(URL,UNAME,UPWD);
         	
         	pstmt = connection.prepareStatement(sql);
         	for(int i=0;i<params.length;i++) {
         		pstmt.setObject(i+1, params[i]);
         	}
         	int count=pstmt.executeUpdate();//返回值表示,增删改几条数据
         	//处理结果
         	if(count>0)
         	{
         		System.out.println("操作成功!!!");
         		flag=true;
         	}
         	
         	}catch(ClassNotFoundException e) {
         		e.printStackTrace();
         	}catch(SQLException e) {
         		e.printStackTrace();
         	}catch(Exception e){
         		e.printStackTrace();
         	}finally {
         		try {
         			//先开的后关,后开的先关
         		if(pstmt!=null)pstmt.close();
         		if(connection !=null)connection.close();
         		}catch(SQLException e) {
         			e.printStackTrace();
         		}finally {
         			
         		}
         	}
    		return flag;
    	 }
    	//查
    	 public static ResultSet executeQuery(String sql,Object [] params) {
    		 
    		     	try {
    		     		
    		     	//a.导入驱动,加载具体的驱动类
    		     	Class.forName("com.mysql.cj.jdbc.Driver");
    		     	//b.与数据库建立连接
    		     	connection = DriverManager.getConnection(URL,UNAME,UPWD);
    		     	
    		     	pstmt = connection.prepareStatement(sql);
    		     	if(params!=null) {
    		     	for(int i=0;i<params.length;i++) {
    		     		pstmt.setObject(i+1, params[i]);
    		     	}
    		     	}
    		     	 rs = pstmt.executeQuery();
    		     	 return rs;
    		     	}catch(ClassNotFoundException e) {
    		     		e.printStackTrace();
    		     		return null;
    		     	}catch(SQLException e) {
    		     		e.printStackTrace();
    		     		return null;
    		     	}catch(Exception e){
    		     		e.printStackTrace();
    		     		return null;
    		     	}
    		     
    		}
    }
    

      

  • 相关阅读:
    Tensorflow 学习
    几种常见损失函数
    两人比赛先选后选谁获胜系列的动态规划问题
    LeetCode 全解(bug free 训练)
    局部敏感哈希LSH
    Annoy解析
    MCMC例子
    TinyBERT简单note
    ALBERT简单note
    求根号2, 网易的一道面试题
  • 原文地址:https://www.cnblogs.com/xk1013/p/13086166.html
Copyright © 2020-2023  润新知