jsp动态改变contenttype值

来源:互联网 发布:淘宝联盟2016旧版 编辑:程序博客网 时间:2024/06/05 18:46

1.新建一个Dynamic Web Project项目,里面新建一个jsp文件,整体的结构框架如图所示:



2.one.jsp文件里面的代码如下所示:

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>example</title></head><body><form action="" method="post"><p>我们在学习使用response动态改变contenttype属性值</p><p><input type="submit" value="word" name="submit" /> <input type="submit" value="excel" name="submit" /><%String str = request.getParameter("submit");if ("word".equals(str)) {response.setContentType("application/msword");} else if ("excel".equals(str)) {response.setContentType("application/vnd.ms-excel");}%></p></form></body></html>

3.在浏览器里面输入http://localhost:8080/meme/one.jsp运行之后的结果如下所示: