1、forward与redirect区别,说一下你知道的状态码,redirect的状态码是多少?

状态码 说明
200  客户端请求成功
302 请求重定向,也是临时跳转,跳转的地址通过Location指定
400 客户端请求有语法错误,不能被服务器识别
403 服务器收到请求,但是拒绝提供服务
404 请求的资源不存在
500 服务器发生不可预期的错误
503 由于临时的服务器维护或者过载,服务器当前无法处理请求。这个状况是临时的,并且将在一段时间以后恢复。如果能够预计延迟时间,那么响应中可以包含一个Retry-After起头用以标明这个延迟时间。如果没有给出这个Retry-After信息,那么客户端应当以处理500(Server Internal Error)响应的方式处理它。注意:503状态码的存在并不意味着必须在服务器过载的时候使用它。某些服务器只不过是希望拒绝某些客户端的连接。

forward和redirect 都是用于信息资源之间的相互转发,不过他们是两种不同的转发法方式。

a).forward:直接转发方式,客户端或者浏览器只发送一次请求,Servlet把请求转发给Servlet、HTML、JSP或其它信息资源,由第2个信息资源响应该请求,两个信息资源共享同一个request对象。

  在Servlet编程中,使用接口RequestDispatcher的forward(ServletRequest, ServletResponse)方法,同时还有另外一个方法是include(ServletRequest request, ServletResponse response),用于包含另一个Servlet的资源;定义如下:

  

public interface RequestDispatcher {// Forwards a request from a servlet to another resource (servlet, JSP file,or HTML file) on the server.public void forward(ServletRequest request, ServletResponse response)throws ServletException, IOException;//  Includes the content of a resource (servlet, JSP page, HTML file) in the response.public void include(ServletRequest request, ServletResponse response)throws ServletException, IOException;}

               

    RequestDispatcher 中两种方法的区别

b).redirect:间接转发方式,服务器端在响应第一次请求的时候,让浏览器再向另外一个URL发出请求,从而达到转发的目的。它本质上是两次HTTP请求,对应两个request对象。状态码是302.

  在Servlet编程中,使用 HttpServletResponse 的 sendRedirect 方法,

public interface HttpServletResponse extends ServletResponse {// Sends a temporary redirect response to the client using the specified redirect location URL.public void sendRedirect(String location) throws IOException;
}

2、servlet生命周期,是否单例,为什么是单例。

  Servlet并不是单例,只是容器让它只实例化一次,变现出来的是单例的效果而已。

   

(1)加载和实例化

  当Servlet容器启动或客户端发送一个请求时,Servlet容器会查找内存中是否存在该Servlet实例,若存在,则直接读取该实例响应请求;如果不存在,就创建一个Servlet实例。

(2) 初始化

  实例化后,Servlet容器将调用Servlet的init()方法进行初始化(一些准备工作或资源预加载工作)。

(3)服务

  初始化后,Servlet处于能响应请求的就绪状态。当接收到客户端请求时,调用service()的方法处理客户端请求,HttpServlet的service()方法会根据不同的请求 转调不同的doXxx()方法。

(4)销毁

  当Servlet容器关闭时,Servlet实例也随时销毁。其间,Servlet容器会调用Servlet 的destroy()方法去判断该Servlet是否应当被释放(或回收资源)。

3、说出Servlet的生命周期,并说出Servlet和CGI的区别。

4、Servlet执行时一般实现哪几个方法?

  1. public void init(ServletConfig config) throws ServletException;
  2. public ServletConfig getServletConfig();
  3. public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException;
  4. public String getServletInfo();
  5. public void destroy();

public interface Servlet {/*** Called by the servlet container to indicate to a servlet that the servlet* is being placed into service.**/public void init(ServletConfig config) throws ServletException;/**** Returns a {@link ServletConfig} object, which contains initialization and* startup parameters for this servlet. The <code>ServletConfig</code>* object returned is the one passed to the <code>init</code> method.** @return the <code>ServletConfig</code> object that initializes this*         servlet** @see #init*/public ServletConfig getServletConfig();/*** Called by the servlet container to allow the servlet to respond to a* request.** <p>* This method is only called after the servlet's <code>init()</code> method* has completed successfully.*/public void service(ServletRequest req, ServletResponse res)throws ServletException, IOException;/*** Returns information about the servlet, such as author, version, and* copyright.**/public String getServletInfo();/*** Called by the servlet container to indicate to a servlet that the servlet* is being taken out of service. This method is only called once all* threads within the servlet's <code>service</code> method have exited or* after a timeout period has passed. After the servlet container calls this* method, it will not call the <code>service</code> method again on this* servlet.*/public void destroy();
}

View Code 源码定义

5、阐述一下阐述Servlet和CGI的区别?

  Servlet 线程处理请求,CGI 对每个请求创建进程。

6、说说Servlet接口中有哪些方法?

public interface Servlet {/*** Called by the servlet container to indicate to a servlet that the servlet* is being placed into service.**/public void init(ServletConfig config) throws ServletException;/**** Returns a {@link ServletConfig} object, which contains initialization and* startup parameters for this servlet. The <code>ServletConfig</code>* object returned is the one passed to the <code>init</code> method.** @return the <code>ServletConfig</code> object that initializes this*         servlet** @see #init*/public ServletConfig getServletConfig();/*** Called by the servlet container to allow the servlet to respond to a* request.** <p>* This method is only called after the servlet's <code>init()</code> method* has completed successfully.*/public void service(ServletRequest req, ServletResponse res)throws ServletException, IOException;/*** Returns information about the servlet, such as author, version, and* copyright.**/public String getServletInfo();/*** Called by the servlet container to indicate to a servlet that the servlet* is being taken out of service. This method is only called once all* threads within the servlet's <code>service</code> method have exited or* after a timeout period has passed. After the servlet container calls this* method, it will not call the <code>service</code> method again on this* servlet.*/public void destroy();
}

View Code 源码定义 五个方法

7、Servlet 3中的异步处理指的是什么?

参考地址: https://www.cnblogs.com/davenkin/p/async-servlet.html

异步处理调用示例:

@WebServlet(urlPatterns = {"/async"}, asyncSupported = true)
public class AsyncServlet extends HttpServlet {private static final long serialVersionUID = 1L;@Overridepublic void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// 开启Tomcat异步Servlet支持req.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);final AsyncContext ctx = req.startAsync();  // 启动异步处理的上下文// ctx.setTimeout(30000);ctx.start(new Runnable() {@Overridepublic void run() {// 在此处添加异步处理的代码
 ctx.complete();}});}
}

8、如何在基于Java的Web项目中实现文件上传和下载?

传统方式:在Servlet中并没有提供文件上传、下载的API,因此我们一般都引入三方组件实现功能,推荐Apache的commons-fileupload,commons-io。

     具体做法是 在前端代码中将需要上传文件的表单类型设置为 enctype="multipart/form-data" ,选择文件的组件为 <input type="file" name="file1">,在Servlet中 使用 Apache 包中的组件接收文件。

Servlet3:在Servlet3中异常简单,前端还是不变,但是接收文件的Servlet只需要加上注解 @MultipartConfig 即可,然后调用 request.getParts() 即可获取所有文件上传的组件并接收文件。

示例参考:

<form action="UploadServlet" method="post" enctype="multipart/form-data">Photo file: <input type="file" name="file" /><input type="submit" value="Upload" />
</form>

//  @MultipartConfig 常用属性maxFileSize单个文件大小,location 文件的临时保存位置,maxRequestSize  文件上传最大请求数
@MultipartConfig(maxFileSize=1000,location = "filePath",maxRequestSize= 2)
@WebServlet("/uploadFile")
public class UploadServlet extends HttpServlet {private static final long serialVersionUID = 1L;@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {resp.setContentType("text/html;charset=UTF-8");PrintWriter out = resp.getWriter();String path = this.getServletContext().getRealPath("/savePath");// 可以用request.getPart()方法获得名为photo的上传附件// 也可以用request.getParts()获得所有上传附件(多文件上传)// 然后通过循环分别处理每一个上传的文件Part part = req.getPart("file");if (part.getSubmittedFileName() != null) {String fileName = part.getSubmittedFileName();part.write(path + "/" + fileName);out.write("上传文件成功");} else {out.write("上传文件失败");}}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {this.doGet(req, resp);}}

View Code 文件上传处理

9、服务器收到用户提交的表单数据,到底是调用Servlet的doGet()还是doPost()方法?

  表单提交的方式是 post 方式,因此经过Servlet的service() 方法处理后,将调用doPost() 方法。

10、Servlet中如何获取用户提交的查询参数或表单数据?

在ServletRequest 接口中定义了若干方法接收用户的查询参数。常用的方法有:

public String getParameter(String name);

public Enumeration<String> getParameterNames();

public String[] getParameterValues(String name);

public Map<String, String[]> getParameterMap();

11、Servlet中如何获取用户配置的初始化参数以及服务器上下文参数? 

// 获取服务器上文的参数
req.getServletContext().getAttribute("sttributeName");

// 获取用户配置的参数
req.getServletContext().getInitParameter("paramName");

12、讲一下redis的主从复制怎么做的?

13、redis为什么读写速率快性能好?

14、redis为什么是单线程?

15、缓存的优点?

16、aof,rdb,优点,区别?

17、redis的List能用做什么场景?

18、说说MVC的各个部分都有那些技术来实现?如何实现?

M:mode,数据模型层,主要负责系统分层中对数据的操作部分,基于DAO模式,我知道的有MyBatis,Hibernate。

V:view,视图解析层,业务逻辑将数据处理完成后,需要展示给前端用户。Jsp, theamleaf

C:controller,请求控制层,针对用户的每个业务请求,都有相应的方法或者Servlet进行处理,通过调用业务逻辑处理和数据操作,返回给用户一个展示数据的页面。SpringMVC,Struts

此外还有具体的业务逻辑处理层,使用Spring Ioc 和 aop 处理对象之间的依赖关系和逻辑。

19、什么是DAO模式?

Data Access Object,主要实现了对数据持久化的操作(数据库的CRUD),DAO模式则是人们大量实践的经验总结,约定俗成,DAO组成主要有:

Database Connection: 数据库的连接和关闭的响应操作。

Pojo :主要是对象的属性,setter/getter方法,这样的每一个pojo对象对应了数据库中的一条记录

Dao: 对数据库操作的接口方法定义,包括CRUD操作;

DaoImpl: 对Dao接口定义的具体实现,粒度为每张的表的各种CRUD操作,包括批量,单个,联表操作等。但是不实现数据库的连接和关闭。

Proxy: 代理类,负责数据库的连接和关闭。

Factory:获取一个DAO对象完成对数据库的相应操作。

20、请问Java Web开发的Model 1和Model 2分别指的是什么?

  参考地址: https://www.breakyizhan.com/javamianshiti/2591.html

Model 1是以页面为中心的Java Web开发,使用JSP+JavaBean技术将页面显示逻辑和业务逻辑处理分开,JSP实现页面显示,JavaBean对象用来保存数据和实现业务逻辑。Model 2是基于MVC(模型-视图-控制器,Model-View-Controller)架构模式的开发模型,实现了模型和视图的彻底分离,利于团队开发和代码复用,如下图所示:

21、你的项目中使用过哪些JSTL标签?

22、使用标签库有什么好处?如何自定义JSP标签?(JSP标签)

转载于:https://www.cnblogs.com/ytuan996/p/10589697.html

javaweb面试一相关推荐

  1. JavaWeb面试(二)

    JavaWeb面试(高级篇) 一.前端面试经典题目 1.说明 HTML 文档中 DTD 的意义和作用(酷讯) DTD,文档类型定义,是一种保证 html 文档格式正确的有效方法,在解析网页时,浏览器将 ...

  2. JavaWeb面试(史上最全的面试介绍,文字内容可以有点枯燥,可以关注一波在慢慢看)

    个人三分钟自述中,考察语言表达能力.社会工作经历.整体形象及其他等四个方面进行评定,自述包括自我介绍和对于此活动的认识理解,自己的能力突出点,自己的活动情况等等 注意细节: 1.直视面试官回答,不要低 ...

  3. javaweb面试问题大全及答案大全,真的太香了!

    整理的70道阿里的Java面试题,都来挑战一下,看看自己有多厉害. 1.java事件机制包括哪三个部分?分别介绍. 2.为什么要使用线程池? 3.线程池有什么作用? 4.说说几种常见的线程池及使用场景 ...

  4. 整合JavaWeb面试过程中相关问题

    内容包括:Servlet.JSP.ajax.JSON.JS.HTML.xml等. 1.Servlet 1.生成动态页面的方法有两种: 1)公共网关接口(common Gateway  Interfac ...

  5. java 导入导出txt文件_Java读取和写入txt文件

    1 问题描述 对于java的读取和写入txt一直心存疑惑,随着知识的积累,又重新进行学习,对java的文件读写理解更加深刻,在这里将自己的小小经验总结分享给大家.下面是大家了解java流的一个基本框架 ...

  6. python求定积分和不定积分_python快速求解不定积分和定积分

    本文首发于微信公众号:"算法与编程之美",欢迎关注,及时了解更多此系列博客. 基本概念 sympy介绍 sympy库的安装非常的简单,利用conda命令可以快速的完成安装. con ...

  7. python下标越界怎么解决_Python异常处理

    问题描述 大家在使用python语言写代码的时候难免会出一些错误,而才入门的朋友们往往不知道是哪里出了错或者不知道自己错在哪里.什么错误. 所以我们要知道是哪行代码出错,其次室错误的类型是什么,错在那 ...

  8. hbuilderx 底部_如何在Hbuilder中制作app底部导航栏

    . 1 问题描述 最近在使用Hbuilder进行移动app前端开发中,我通常搭建首页框架的常规方法是在index.html主文件中使用多种框架组件模块,再通过css叠层样式表对相应模块加以修饰.但在分 ...

  9. java中指数函数的使用方法图解,基本初等函数 指数函数 代码篇

    本文首发于微信公众号:"算法与编程之美",欢迎关注,及时了解更多此系列博客. 由于机器学习和数学密切相关,尤其是数学中的函数,因此我们非常有必要复习和了解基本的函数知识.上一篇文章 ...

最新文章

  1. 微信服务通知消息找回_第三方平台微信服务号模板消息怎么发送
  2. apue 第4章 文件和目录
  3. 天翼云从业认证(1.6)虚拟化技术基础、服务器虚拟化、存储虚拟化和网络虚拟化技术;
  4. 论文推荐 | 2018中国卫星导航年会论文集
  5. Memcache存储大数据的问题(大于1m)
  6. 星期三,今天早上上了四节JS课程,下午听健康讲座,晚上装系统
  7. c语言编译器app官网下载,c语言编译器
  8. 让你python代码更快的3个小技巧
  9. html body最小高度,CSS网页布局中的最小高度问题的解决方法
  10. AutoItLibrary安装和常见问题解决
  11. Windows命令查看文件MD5
  12. 单尺度Retinex算法学习
  13. Win10安装程序修复计算机,directx修复工具win10最新版
  14. Ubantu18.04环境下编译android源码
  15. [CF1504E]Travelling Salesman Problem
  16. 伟大的UHD编解码器的辩论:谷歌VP9与HEVC / H.265
  17. Javascript捕捉(capturing)与冒泡(bubbling)的区别
  18. “智多星”智能手机销售网
  19. ppt矩形里面的图片怎么放大缩小_如何使用PPT调节图片的大小
  20. 解决Stm32出现..\HARDWARE\ADC\adc.c(22): error: #20: identifier ADC_InitTypeDef is undefined异常

热门文章

  1. mysql5.7文本编辑器_Windows下mysql-5.7.28下载、安装、配置教程
  2. php帝国下载文件,帝国CMS如何支持弹出下载txt jpg等格式
  3. 简单 描述oracle 存储结构,下面的各选项中哪一个正确描述了
  4. python 静态方法 类方法 的作用_Python实例方法、类方法、静态方法的区别与作用详解...
  5. 阿里云centos 7.6安装mysql_阿里云服务器中Linux下centos7.6安装mysql8.0.11
  6. Elasticsearch介绍Kibana分词器增删改操作
  7. SpringCloud服务注册启动的时候报错(com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException)
  8. 第十六届智能车竞赛过程中都发生了什么:怎么感到今年更难呢?
  9. 2021年春季学期-信号与系统-第七次作业参考答案-第八小题
  10. 端口保护:如果你不把我当回事,我就会让你好看