zoukankan      html  css  js  c++  java
  • JSP&Servlet面试题

    1,使用post方式提交的时候,如何处理中文乱码?

    request.setCharacterEncoding(“UTF-8”);//注意UTF大写

     2,使用get方式提交的时候,如何处理中文乱码

    String newname=new String(request.getParameter(“username”).getBytes(“ISO-8859-1”),”UTF-8”)

    3,使用过滤器如何处理中文乱码

    public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {

    request.setCharacterEncoding("UTF-8");

    chain.doFilter(request, response);//将请求传回过滤器

    }

    4,EL表达式中四个作用域是什么?

    page<request<session<application

    5,举例JSTL标签中有几个通用标签,几个逻辑标签,几个迭代标签

    分别写代码。

    通用标签:out, remove,set   逻辑标签:if   迭代标签:foreach

    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>//使用标签前要导入uri

    <c:set  var=”变量名 ”  value=”变量值“  scope=”作用域”></c:set>  //定义变量

    <c:remove var=”变量名” scope=”作用域”></c:remove>  //删除变量

    <c:out value=”${sessionScope.str}“ ></c:out>  //显示变量

    <c:froeach items=”变量值” val=”变量名”></c:foreach>//迭代

    <c:if  test=${条件}></c:if>//逻辑

    6,使session失效的方法是什么。删除session中的值是哪个方法?

    失效:调用HttpSession的方法:session.setMaxInactiveInterval(1);

    删除:session.removeAttribute(String key)

    7,我们学过一个在JSP页面写入的格式化标签是哪个?

    <%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

    8.输出0-100能被3整除的数:使用JSTL

    <c:forEach var="s" begin="1" end="100" step="1">

    <c:if test="${s%3==0}">

    <c:out value="${s }"></c:out>

    </c:if>

    </c:forEach>

    9.什么是CookieCookie有几个常用的方法,举例说明:

    CookieWeb服务器保存在客户端的一系列文本信息

    Cookie cookie=new Cookie("password","haha123456");//键值对的形式

    cookie.setMaxAge(2*60);//设置有效期(秒)

    response.addCookie(cookie);//添加cookie

    10.JSP有几种组成元素,简写每一个组成元素的语法格式

    jsp的组成部分及介绍:
    1.模板元素htmlxml
    2.注释元素
    (1)html注释,显示在客户端源码中
    <!-- 注释 -->
    (2)jsp注释,不会显示在客户端源码,只在jsp
    <% -- 注释 --%>
    (3)单行注释与多行注释
    单行// 多行/** 注释 */
    3.指令元素
    (1)page指令
    <%@page %>
    (2)include指令
    包含代码,静态包含
    <%@include file=""%>
    (3)taglib
    <%@taglib prefix="c" uri="taglibURI" %>
    标签前缀 找到标签描述文件和标签库的方式
    4.脚本元素
    (1)声明
    <%! 方法或者变量%>
    被转换成servlet中的实例属性和实例方法
    (2)表达式
    <%=%>
    例:<%="你好,世界" %>
    相当于jsp中的out.println("你好,世界");
    转化成servlet中的out.print("你好,世界" );
    (3)scriptlets
    <% %>
    多个scriptlets合成一个被包含在servletservice方法中
    5.动作元素
    1.<jsp:param>
    以名值对的形式为其他标签提供附加信息
    <jsp:param name="" value=""/>
    2.<jsp:include>
    包含的是结果,两个文件
    <!--flush属性必须为true-->
    <jsp:include page="" flush="true"/>
    或者
    <jsp:include page="" flush=""true">
    <jsp:param name="" value=""/>
    </jsp:include>
    3<jsp:forward>
    请求转发,每当遇到此操作时,就停止执行当前的jsp,转而执行被转发的资源。
    <jsp:forward page=""/>
    或者
    <jsp:forward page="">
    <jsp:param name="" value=""/>
    </jsp:forward>
    4.<jsp:useBean><jsp:setProperty><jsp:getProperty>
    <jsp:useBean id="id" scope="page|request|session|application" class="">
    <jsp:setProperty name="id" property="*"/>
    </jsp:useBean>
    <jsp:getProperty name="id" property="属性名"/>
    5.<jsp:plugin>
    可以使用它来插入Applet或者JavaBean

    11.简答题:分别介绍JSPServlet的生命周期

    servlet生命周期:实例化,初始化,调用(就绪),销毁。

    12.如下哪些方法能实现session对象ID的超连接方式的URL重写?

     //请教大神回复

    13.请描述HttpServletResponse对象的作用?列出两个常用方法和说明?

    作用:1.设置相应字符集,2.向客户端输出信息

    Response.sendredirect(“”);//重定向

    Response.setContentType(“text/html;charset=UTF-8”);

    14.请描述HttpServletRequest对象的作用?列出两个常用方法和说明?

    作用:1.提取客户端请求信息,2.修改字符集,3.在服务器端保存值

    void setAttribute(String key,Object value)

    request作用域中保存数据,键为key,值为value

    Object getAttribute (String key)

    request作用域查找键值为key的数据对象的值

    void removeAttribute (String key)

    删除request作用域内键值为key的数据对象

    Enumeration getAttributeNames ()

    返回request作用域内保存的所有数据对象的键值的集合

    15.简述JSP的常见的内置对象

    1request对象:

    2response对象:

     3session对象:

    4out对象:

    5page对象:

    6application对象:

    7exception对象:

    8pageContext对象:

    9config对象:

    16.简单介绍Cookie 如何保存到客户端的浏览器中。

    手写代码

    Cookie cookie=new Cookie("password","haha123456");//键值对的形式

    cookie.setMaxAge(2*60);//设置有效期(秒)

    response.addCookie(cookie);//添加cookie 

    17.处理中文乱码的几种方式,举例说明,最少3种。

    request.setcharacterencoding(“UTF-8”);//请求房post

    response.setContentType(“text/html;charset=UTF-8”);// 相应方

    string  newname=new string("text/html;charset=UTF-8");//get方式

  • 相关阅读:
    Leetcode 538. Convert BST to Greater Tree
    Leetcode 530. Minimum Absolute Difference in BST
    Leetcode 501. Find Mode in Binary Search Tree
    Leetcode 437. Path Sum III
    Leetcode 404. Sum of Left Leaves
    Leetcode 257. Binary Tree Paths
    Leetcode 235. Lowest Common Ancestor of a Binary Search Tree
    Leetcode 226. Invert Binary Tree
    Leetcode 112. Path Sum
    Leetcode 111. Minimum Depth of Binary Tree
  • 原文地址:https://www.cnblogs.com/sunda847882651/p/9470879.html
Copyright © 2011-2022 走看看