java开发案例
⑴ java实现拦截HTTP请求的几种方式
在Java的服务端开发当中,拦截器是很常见的业务场景,这里对Java开发当中几种常见的拦截器的实现方式进行记录和分析。案例说明基于Spring Boot环境。
一:实现javax.servlet.Filter接口(使用过滤器方式拦截请求)
import org.springframework.stereotype.Component;import javax.servlet.*;import java.io.IOException;import java.util.Date;@Componentpublic class TimeInterceptor implements Filter {@Overridepublic void init(FilterConfig filterConfig) throws ServletException {System.out.println("time filter init");}@Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {System.out.println("time filter start");long start = new Date().getTime();filterChain.doFilter(servletRequest, servletResponse);System.out.println("time filter 耗时:"+(new Date().getTime()-start));System.out.println("time filter finish");}@Overridepublic void destroy() {System.out.println("time filter destroy");}}
如使用@Compent注解声明不需要加入其它配置即可使得拦截器生效,但是默认拦截/*,会拦截所有请求。
二:使用@Bean注入自定义拦截器,依然上面的代码,去掉@Compent注解,创建TimeWebConfig配置类:
import org.springframework.boot.web.servlet.FilterRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import java.util.ArrayList;import java.util.List;@Configurationpublic class TimeWebConfig {@Beanpublic FilterRegistrationBean timeFilter(){FilterRegistrationBean registrationBean = new FilterRegistrationBean();TimeInterceptor interceptor = new TimeInterceptor();registrationBean.setFilter(interceptor);List<String> urls = new ArrayList<>();urls.add("/user/*");registrationBean.setUrlPatterns(urls);return registrationBean;}}
上面这两种拦截请求的实现是基于JavaEE提供的Filter接口实现的,缺点在于,该拦截器实际上是一个过滤器,执行代码的方法doFilter只提供了request,response等参数,当请求进入被过滤器拦截的时候,我们并不知道这个请求是由哪个控制器的哪个方法来执行的。
三:使用springMVC提供的拦截器,实现org.springframework.web.servlet.HandlerInterceptor接口:
创建自定义的拦截器:
import org.springframework.stereotype.Component;import org.springframework.web.method.HandlerMethod;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.util.Date;@Componentpublic class MyInterceptor implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler) throws Exception {System.out.println("preHandler");System.out.println(((HandlerMethod) handler).getBean().getClass().getName());System.out.println(((HandlerMethod) handler).getMethod().getName());httpServletRequest.setAttribute("start", new Date().getTime());return true;}@Overridepublic void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {System.out.println("postHandler");Long start = (Long) httpServletRequest.getAttribute("start");System.out.println("time interceptor 耗时:"+(new Date().getTime()-start));}@Overridepublic void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {System.out.println("afterCompletion");Long start = (Long) httpServletRequest.getAttribute("start");System.out.println("time interceptor 耗时:"+(new Date().getTime()-start));System.out.println("ex is:"+e);}}
创建配置类:
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;@Configurationpublic class WebConfig extends WebMvcConfigurerAdapter {@Autowiredprivate MyInterceptor interceptor;@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(interceptor).addPathPatterns("/user/*").excludePathPatterns("/blog/*");}}
此种方式的拦截器当中我们能够获取拦截的请求对应的类和方法的相关信息,缺点在于该handler对象无法获取具体执行方法的参数信息。
四:利用Spring的切面(AOP)实现拦截器:
引入jar包:
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-aop --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency>
创建切片类:
import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.springframework.stereotype.Component;import java.util.Date;@Aspect@Componentpublic class TimeAspect {@Around("execution(* com.qinker.controller.UserController.*(..))")public Object handlerControllerMethod(ProceedingJoinPoint point) throws Throwable {System.out.println("time aspect start");long start = new Date().getTime();Object[] args = point.getArgs();for (Object obj : args) {System.out.println("arg is:"+obj);}Object obj = point.proceed();//具体方法的返回值System.out.println("aspect 耗时:"+(new Date().getTime()-start));System.out.println("time aspect end");return obj;}}
aspectj基于AOP实现的拦截器功能十分强大,具体详解请参考spring官网网站的文档。
⑵ java经典编程案例有哪些
java编程的记事本:
import java.util.*;
public class JieChengExample
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
int n,sum;
Jiecheng jie=new Jiecheng();
System.out.print("输入n的值:");//输入有几个阶乘相加
n=input.nextInt();
sum=0;
for(int i=1;i<=n;i++)
{
sum=sum+jie.jiecheng(i);//这是n个阶乘相加
}
System.out.println("1!+2!+3!+....+n!的和是:"+sum);
}
}
class Jiecheng
{
public int jiecheng(int temp)//算阶乘的方法
{
int sum=1;
for(int i=1;i<=temp;i++)
{
sum=sum*i; //计算阶乘
}
return sum;//将一个阶乘返回
}
}
2.java赛马游戏:
import java.util.Random;
public class Test {
public static void main(String[] args) {
Competition c = new Competition();
Thread T = new Thread(c);
T.start();
}
}
class Competition implements Runnable{
int red = 0;
int green = 0;
int Speed [] = new int [2];
Competition(){
}
public void run(){
Random r = new Random();
for(int a= 0;a<500;a++){
for(int j = 0;j<2;j++){
Speed[j] = r.nextInt(2);
red = red + Speed[j];
Speed[j] = r.nextInt(2);
green = green + Speed[j];
}
System.out.println("red的速度为"+red);
System.out.println("green的速度为"+green);
while(red >=500 || green>=500){
if(red >=500){
System.out.println("red先抵达终点线");
}
if(green >= 500){
System.out.println("green先抵达终点线");
}
if(green ==500 && red ==500 ){
System.out.println("两个同时到达");
}
return;
}
}
/* if(red >green){
System.out.println("Redwin"+red);
}
if(red<green){
S...import java.util.Random;
public class Test {
public static void main(String[] args) {
Competition c = new Competition();
Thread T = new Thread(c);
T.start();
}
}
class Competition implements Runnable{
int red = 0;
int green = 0;
int Speed [] = new int [2];
Competition(){
}
public void run(){
Random r = new Random();
for(int a= 0;a<500;a++){
for(int j = 0;j<2;j++){
Speed[j] = r.nextInt(2);
red = red + Speed[j];
Speed[j] = r.nextInt(2);
green = green + Speed[j];
}
System.out.println("red的速度为"+red);
System.out.println("green的速度为"+green);
while(red >=500 || green>=500){
if(red >=500){
System.out.println("red先抵达终点线");
}
if(green >= 500){
System.out.println("green先抵达终点线");
}
if(green ==500 && red ==500 ){
System.out.println("两个同时到达");
}
return;
}
}
/* if(red >green){
System.out.println("Redwin"+red);
}
if(red<green){
System.out.println("Greenwin"+green);
}
if(red == green){
System.out.println("equal");*/
JAVA的介绍:
Java是一种可以撰写跨平台应用程序的面向对象的程序设计语言。Java技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于PC、数据中心、游戏控制台、科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。
⑶ 昌平北大青鸟培训班java项目开发案例分享
昌平北大青鸟作为中国高端职业教育第一品牌,拥有着顶尖的java教学团队和高端的java课程,更有着丰富的java实训项目,下面简单介绍一下在昌平北大青鸟java培训过程中所实训洞贺的java项目有哪些?
1.开班前5天训练营将完成“捕鱼达人”游戏项销败目
2.开班2周内,贯穿着Java语言基础及Java面向对象的学习,学员将完成专业版的俄罗斯方块项目(Tetris)
3.开班一个半月内,贯穿JavaSE及数据库的学习,将完成T-DMS数据采集系统项目
整个系统由DMS采集端、DMS服务器端和NetCTOSS服务器端三部分组成。DMS采集端的主要任务是采集、解析、匹配和保存并发送登录数据。DMS采集端程序采集、解析获得数据后,将处理好的数据发送到DMS服务器端。DMS服务器端接收到日志数据,将日志数据备份保存为文件数据,并存入数据库。
4.在学习第3个月内,贯穿Web前端技术和Servlet/JSP的学习,将完成T-NetCTOSS项目
NetCTOSS系统的主要任务是提供对于数据的计费查询和管理功能。
5.开班4个月内,学员将完成“航空订票”真纳斗派实项目