第一范文网 - 专业文章范例文档资料分享平台

《JavaWeb程序开发入门》课后练习(含答案)只是分享

来源:用户分享 时间:2025/7/31 8:32:39 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

public class LastAccessServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request,

HttpServletResponse response)throws ServletException, IOException { /*

* 设定一个 cookie 的name : lastAccessTime * 读取客户端发送cookie 获得用户上次的访问时间显示*/ String lastAccessTime = null;

// 获取所有的cookie,并将这些cookie存放在数组中 Cookie[] cookies = request.getCookies();

for (int i = 0; cookies != null && i < cookies.length; i++) { if (\// 如果cookie的名称为lastAccess,则获取该cookie的值 lastAccessTime = cookies[i].getValue(); break; } }

// 判断是否存在名称为lastAccess的cookie if (lastAccessTime == null) {

response.getWriter().print(\您是首次访问本站!!!\} else {

response.getWriter().print(\您上次的访问时间\}

// 创建cookie,将当前时间作为cookie的值发送给客户端

Cookie cookie = new Cookie(\cookie.setMaxAge(60*60);//保存1小时 //访问chapter06下资源时回送cookie cookie.setPath(\// 发送 cookie

response.addCookie(cookie); } }

response.setContentType(\

------第2题答案------ 步骤如下:

1)创建一个PurchaseServlet类,使其继承HttpServlet类并重写该类的doGet()方法。

2)在doGet()方法中,使用request.getSession()实现购物车。 3)将session的id保存到cookie中。 4)重定向到购物车页面。 具体代码实现如下:

public class PurchaseServlet extends HttpServlet { public void doGet(HttpServletRequest req,

HttpServletResponse resp)

throws ServletException, IOException {

// 获得用户购买的商品

String id = req.getParameter(\if (id == null) {

// 如果id为null,重定向到ListBookServlet页面 String url = \resp.sendRedirect(url); return; }

Book book = BookDB.getBook(id); // 创建或者获得用户的Session对象

HttpSession session = req.getSession(); // 从Session对象中获得用户的购物车 List cart = (List)

session.getAttribute(\

if (cart == null) {

// 首次购买,为用户创建一个购物车(List集合模拟购物车) cart = new ArrayList(); // 将购物城存入Session对象

session.setAttribute(\}

// 将商品放入购物车 cart.add(book);

// 创建Cookie存放Session的标识号

Cookie cookie = new Cookie(\cookie.setMaxAge(60 * 30); cookie.setPath(\resp.addCookie(cookie); // 重定向到购物车页面

String url = \resp.sendRedirect(url); } }

第七章

【测一测】

1、 请使用include标签编写两个JSP页面,

要求:输出b.jsp页面的内容,等待5秒,再输出a.jsp页面。 2、 已知一个datetime.jsp页面用于显示当前时间。请编写jsp

文件用于显示“欢迎来到传智播客,现在的时间是:”+当前时间。

------第1题答案------

a.jsp代码:

<%@ page contentType=\<%Thread.sleep(5000);%> a.jsp内的中文

b.jsp代码:

<%@ page contentType=\b.jsp内的中文

------第2题答案------

<%@page language=\

欢迎你

欢迎来到传智播客,现在的时间是: <%@ include file=\

第八章

【测一测】

1、请编写一个类,实现通过对象得到完整的“包.类”名称的功能。 2、设计一个程序使用BeanUtils工具为Person对象(JavaBean类)赋值。 1)直接生成User对象。

2)使用BeanUtils工具为name属性赋值”youjun”,age赋值为31。 3)使用BeanUtils工具取出属性值,并在控制台输出。

------第1题答案------

package cn.itcast.javabean; class Cs{}

public class GetClassNameDemo { }

public static void main(String[] args) { }

Cs cs = new Cs ();

System.out.println(Cs. getClass().getName());

------第2题答案------

package cn.itcast.chapter08.beanutils;

import org.apache.commons.beanutils.BeanUtils; import cn.itcast.chapter08.javabean.Person; public class BeanUtilsDemo01 {

public static void main(String[] args) throws Exception{

Person p = new Person(); //使用BeanUtils为属性赋值

《JavaWeb程序开发入门》课后练习(含答案)只是分享.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c1gc1g8gbji3gzju6vsv034ka295j0v00cvi_3.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top