JSP作業(yè)和例子:map,put,EL表達式,下拉框selected,單選框checked【詩書畫唱】
1、創(chuàng)建一個商品類,包含商品名稱和商品價格兩個屬性,在a.jsp頁面創(chuàng)建一個<String,Product>類型的map,添加三樣商品,跳轉(zhuǎn)到show.jsp頁面中通過EL表達式顯示map中所有商品的名稱和價格信息。

package com.SSHC;
public class Product {
? ? private String name;
? ? private Double price;
? ? private String type;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}


<%@page import="com.SSHC.Product"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html;?
charset=UTF-8" pageEncoding="UTF-8"%>
<%
? ? String path = request.getContextPath();
? ? String basePath = request.getScheme()+"://"
? ? +request.getServerName()+":"+request.getServerPort()+path+"/";
?
?
? ? Map<String,Product>m = new HashMap<String,Product>();
? ? Product P = new Product();
? ? P.setName("詩書畫唱");
? ? P.setPrice(66.6);
? ? m.put("Key", P);
? ? request.setAttribute("m", m);
request.getRequestDispatcher("b.jsp").forward(request, response);
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? ? <head>
? ? ? ? <base hreff="<%=basePath%>">
? ? ? ? <title></title>
? ? ? ? <meta http-equiv="pragma" content="no-cache">
? ? ? ? <meta http-equiv="cache-control" content="no-cache">
? ? ? ? <meta http-equiv="expires" content="0">
? ? ? ? <meta http-equiv="keywords" content="keyword1,
? ? ? ? keyword2,keyword3">
? ? ? ? <meta http-equiv="description" content="This is my page">
? ? </head>
? ? <body>
? ? ?
? ? </body>
</html>


<%@ page language="java" contentType="text/html; charset=utf-8"
? ? pageEncoding="utf-8"%>? ?
? ? <%request.getAttribute("m");?
%>
<!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=ISO-8859-1">
<title>Insert title here</title></head>
<body>
??
? ? ? ? <h1>${m["Key"].name }</h1>
? ? ? ? <h1>${m["Key"]["price"] }</h1>
</body>
</html>


2、創(chuàng)建一個Person類,包含姓名,性別,學(xué)歷三個屬性,在b.jsp頁面創(chuàng)建一個Person對象,跳轉(zhuǎn)到result.jsp頁面后通過EL表達式顯示姓名,性別和學(xué)歷(注意:學(xué)歷是一個下拉框)
<select>
? ? <option>初中</option>
? ? <option>高中</option>
? ? <option>大專</option>
? ? <option>本科</option>
</select>

package com.SSHC;
public class Person {
private String name;
private Integer age;
private String xueLi;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getXueLi() {
return xueLi;
}
public void setXueLi(String xueLi) {
this.xueLi = xueLi;
}
?
}


<%@page import="java.util.Date"%>
<%@page import="com.SSHC.Person"%>
<%@ page language="java" contentType="text/html;
?charset=UTF-8" pageEncoding="UTF-8"%>
<%
? ? String path = request.getContextPath();
? ? String basePath = request.getScheme()+"://"
? ? +request.getServerName()+":"+request.getServerPort()+path+"/";
? ? Person s = new Person();
? ? s.setName("詩書畫唱");
? ? s.setSex("男");
? ? s.setXueLi("本科");
? ?
? ? request.setAttribute("KeyS",s);
request.getRequestDispatcher("result.jsp").forward(request, response);
? ?
%>


<%@ page language="java" contentType="text/html;?
charset=UTF-8" pageEncoding="UTF-8"%>
<%
? ? String path = request.getContextPath();
? ? String basePath = request.getScheme()+"://"
? ? +request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? ? <head>
? ? ? ? <base hreff="<%=basePath%>">
? ? ? ? <title></title>
? ? ? ? <meta http-equiv="pragma" content="no-cache">
? ? ? ? <meta http-equiv="cache-control" content="no-cache">
? ? ? ? <meta http-equiv="expires" content="0">
? ? ? ? <meta http-equiv="keywords" content=
? ? ? ? "keyword1,keyword2,keyword3">
? ? ? ? <meta http-equiv="description" content="This is my page">
? ? </head>
? ? <body>? ? ? ?
? ? ? ? <form action="" method="post">
? ? ? ? ? ? <label>姓名:</label><input type=
? ? ? ? ? ? "text" value="${KeyS.name }" />
? ? ? ? ? ? <br>
? ? ? ? ? ?
? ? ? ? ? ? <label>性別:</label>
? ? ? ? ? ? <input type="radio" name="ssex" value="男"?
? ? ? ? ? ? ? ? ${KeyS.sex=="男" ? "checked":"" } />男
? ? ? ? ? ? <input type="radio" name="ssex" value="女"?
? ? ? ? ? ? ? ? ${KeyS.sex=="女" ? "checked":"" } />女
? ? ? ? ? ? <br>
? ? ? ? ? ? <label>學(xué)歷:</label>
? ? ? ? ? ?<select>
? ? <option? ${KeyS.xueLi=="初中" ? "selected":"" }>初中</option>
? ? <option? ${KeyS.xueLi=="高中" ? "selected":"" }>高中</option>
? ? <option? ${KeyS.xueLi=="大專" ? "selected":"" }>大專</option>
? ? <option? ${KeyS.xueLi=="本科" ? "selected":"" }>本科</option>
</select>? ? ? <br>??
? ? ? ? </form> </body></html>





3、創(chuàng)建一個Cat類,包含名字和毛色,在c.jsp創(chuàng)建一個Cat對象,跳轉(zhuǎn)到my.jsp頁面,通過EL表達式顯示出Cat的名字和毛色。

package com.SSHC;
public class Cat {
private? String name;
private? String? maoSe;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMaoSe() {
return maoSe;
}
public void setMaoSe(String maoSe) {
this.maoSe = maoSe;
}
}


<%@page import="java.util.Date"%>
<%@page import="com.SSHC.Cat"%>
<%@ page language="java" contentType="text/html;
?charset=UTF-8" pageEncoding="UTF-8"%>
<%
? ? String path = request.getContextPath();
? ? String basePath = request.getScheme()+"://"
? ? +request.getServerName()+":"+request.getServerPort()+path+"/";
? ? Cat s = new Cat();
? ? s.setName("詩書畫唱的可愛貓貓");
? ? s.setMaoSe("白色");
? ?
? ? request.setAttribute("KeyS",s);
request.getRequestDispatcher("my.jsp").forward(request, response);
? ?
%>


<%@ page language="java" contentType="text/html;?
charset=UTF-8" pageEncoding="UTF-8"%>
<%
? ? String path = request.getContextPath();
? ? String basePath = request.getScheme()+"://"
? ? +request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? ? <head>
? ? ? ? <base hreff="<%=basePath%>">
? ? ? ? <title></title>
? ? ? ? <meta http-equiv="pragma" content="no-cache">
? ? ? ? <meta http-equiv="cache-control" content="no-cache">
? ? ? ? <meta http-equiv="expires" content="0">
? ? ? ? <meta http-equiv="keywords" content=
? ? ? ? "keyword1,keyword2,keyword3">
? ? ? ? <meta http-equiv="description" content="This is my page">
? ? </head>
? ? <body>? ? ? ?
? ? ? ? <form action="" method="post">
? ? ? ? ? ? <label>姓名:</label><input type=
? ? ? ? ? ? "text" value="${KeyS.name }" />
? ? ? ? ? ? <br>
? ? ? ? ? ?
? ? ? ? ? ? <label>毛色:</label><input type=
? ? ? ? ? ? "text" value="${KeyS.maoSe }" />
? ? ? ? ? ? <br>
? ? ? ? </form> </body></html>


——————————————————————
例子(含自己寫的題目內(nèi)容):
聲明Map<String,String>map,?Student 類,把內(nèi)容放進去,之后用EL表達式打印出來(request.setAttribute("fruitMap", map);,?map.put("4號", "石榴");
中的"fruitMap"就像鑰匙,用${fruitMap["4號"] }“打開”(打?。?span id="s0sssss00s" class="font-size-23">"石榴"(map的值)這扇門:

<%@page import="com.SSHC.Student"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html;?
charset=UTF-8" pageEncoding="UTF-8"%>
<%
? ? String path = request.getContextPath();
? ? String basePath = request.getScheme()+"://"
? ? +request.getServerName()+":"+request.getServerPort()+path+"/";
? ? Map<String,String>map = new HashMap<String,String>();
? ? map.put("1號", "蘋果");
? ? map.put("2號", "火龍果");
? ? map.put("4號", "石榴");
? ? map.put("7號", "西梅");
? ? request.setAttribute("fruitMap", map);
? ??
? ? Map<String,Student>m = new HashMap<String,Student>();
? ? Student s1 = new Student();
? ? s1.setSname("張三");
? ? m.put("20190001", s1);
? ? request.setAttribute("m", m);
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? ? <head>
? ? ? ? <base hreff="<%=basePath%>">
? ? ? ? <title></title>
? ? ? ? <meta http-equiv="pragma" content="no-cache">
? ? ? ? <meta http-equiv="cache-control" content="no-cache">
? ? ? ? <meta http-equiv="expires" content="0">
? ? ? ? <meta http-equiv="keywords" content="keyword1,
? ? ? ? keyword2,keyword3">
? ? ? ? <meta http-equiv="description" content="This is my page">
? ? </head>
? ? <body>
? ? ? ? <h1>${fruitMap["4號"] }</h1>
? ? ? ? <h1>${m["20190001"].sname }</h1>
? ? ? ? <h1>${m["20190001"]["sname"] }</h1>
? ? </body>
</html>


package com.SSHC;
import java.util.Date;
public class Student {
private String sname;
private String hobby;
private Date birth;
private char ssex;
private String scls;
public Student(){
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public char getSsex() {
return ssex;
}
public void setSsex(char ssex) {
this.ssex = ssex;
}
public String getScls() {
return scls;
}
public void setScls(String scls) {
this.scls = scls;
}
public Student(String sname, String hobby,?
Date birth, char ssex, String scls) {
super();
this.sname = sname;
this.hobby = hobby;
this.birth = birth;
this.ssex = ssex;
this.scls = scls;
}
@Override
public String toString() {
return "Student [sname=" + sname +?
", hobby=" + hobby + ", birth=" + birth
+ ", ssex=" + ssex + ", scls=" + scls + "]";
}
}


用上
? request.setAttribute("stu", s);
? ? request.setAttribute("prop", "ssex");,通過?${stu[prop] }獲取?"ssex"的內(nèi)容

<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="com.SSHC.Student"%>
<%@ page language="java" contentType="text/html;?
charset=UTF-8" pageEncoding="UTF-8"%>
<%
? ? String path = request.getContextPath();
? ? String basePath = request.getScheme()+"://"
? ? +request.getServerName()+":"+request.getServerPort()+path+"/";
? ? Student s = new Student();
? ? s.setSname("Kite");
? ? s.setHobby("唱歌,跳舞");
? ? String strBirth = "2000-11-20";
? ? //將strBirth字符串轉(zhuǎn)換成日期對象
? ? SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
? ? Date birth = sdf.parse(strBirth);? ?
? ? s.setBirth(birth);
? ? s.setSsex('女');
? ? s.setScls("J190802");
? ? request.setAttribute("stu", s);
? ? request.setAttribute("prop", "ssex");
? ? //跳轉(zhuǎn)到result.jsp
? ? request.getRequestDispatcher("result.jsp").
? ? forward(request, response);
%>


<%@ page language="java" contentType="text/html;?
charset=UTF-8" pageEncoding="UTF-8"%>
<%
? ? String path = request.getContextPath();
? ? String basePath = request.getScheme()+"://"
? ? +request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? ? <head>
? ? ? ? <base hreff="<%=basePath%>">
? ? ? ? <title></title>
? ? ? ? <meta http-equiv="pragma" content="no-cache">
? ? ? ? <meta http-equiv="cache-control" content="no-cache">
? ? ? ? <meta http-equiv="expires" content="0">
? ? ? ? <meta http-equiv="keywords" content="keyword1,
? ? ? ? keyword2,keyword3">
? ? ? ? <meta http-equiv="description" content="This is my page">
? ? </head>
? ? <body>
? ? ? ? ${stu[prop] }
? ? </body>
</html>


用上頁面跳轉(zhuǎn),要求把改為String? sex(這里用char的話容易報錯)的Student的內(nèi)容跳轉(zhuǎn)并打印到別的界面,性別用單選框顯示:

package com.SSHC;
import java.util.Date;
public class Student {
private String sname;
private String hobby;
private Date birth;
private String ssex;
private String scls;
public Student(){
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public String getSsex() {
return ssex;
}
public void setSsex(String ssex) {
this.ssex = ssex;
}
public String getScls() {
return scls;
}
public void setScls(String scls) {
this.scls = scls;
}
}


<%@page import="java.util.Date"%>
<%@page import="com.SSHC.Student"%>
<%@ page language="java" contentType="text/html;
?charset=UTF-8" pageEncoding="UTF-8"%>
<%
? ? String path = request.getContextPath();
? ? String basePath = request.getScheme()+"://"
? ? +request.getServerName()+":"+request.getServerPort()+path+"/";
? ? Student s = new Student();
? ? s.setSname("詩書畫唱");
? ? s.setHobby("當(dāng)UP主");
? ? s.setBirth(new Date());
? ? s.setSsex("男");
? ? s.setScls("詩書畫唱班");
? ? request.setAttribute("stu",s);
request.getRequestDispatcher("update.jsp").forward(request, response);
? ?
%>


<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
? ? String path = request.getContextPath();
? ? String basePath = request.getScheme()+"://"
? ? +request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? ? <head>
? ? ? ? <base hreff="<%=basePath%>">
? ? ? ? <title></title>
? ? ? ? <meta http-equiv="pragma" content="no-cache">
? ? ? ? <meta http-equiv="cache-control" content="no-cache">
? ? ? ? <meta http-equiv="expires" content="0">
? ? ? ? <meta http-equiv="keywords" content=
? ? ? ? "keyword1,keyword2,keyword3">
? ? ? ? <meta http-equiv="description" content="This is my page">
? ? </head>
? ? <body>? ? ? ?
? ? ? ? <form action="" method="post">
? ? ? ? ? ? <label>姓名:</label><input type=
? ? ? ? ? ? "text" value="${stu.sname }" />
? ? ? ? ? ? <br>
? ? ? ? ? ? <label>愛好:</label><input type=
? ? ? ? ? ? "text" value="${stu.hobby }" />
? ? ? ? ? ? <br>
? ? ? ? ? ? <label>性別:</label>
? ? ? ? ? ? <input type="radio" name="ssex" value="男"?
? ? ? ? ? ? ? ? ${stu.ssex=="男" ? "checked":"" } />男
? ? ? ? ? ? <input type="radio" name="ssex" value="女"?
? ? ? ? ? ? ? ? ${stu.ssex=="女" ? "checked":"" } />女
? ? ? ? ? ? <br>
? ? ? ? ? ? <label>生日:</label>
? ? ? ? ? ? <input type="text" value="${stu.birth }" />? ? ? ? ??
? ? ? ? </form>
? ? </body>
</html>


用SimpleDateFormat顯示日期到網(wǎng)頁:

<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="com.SSHC.Student"%>
<%@ page language="java" contentType="text/html;?
charset=UTF-8" pageEncoding="UTF-8"%>
<%
? ? String path = request.getContextPath();
? ? String basePath = request.getScheme()+
? ? "://"+request.getServerName()+
? ? ":"+request.getServerPort()+path+"/";
? ? Student s = new Student();
? ? s.setSname("Kite");
? ? s.setHobby("唱歌,跳舞");
? ? String strBirth = "2000-11-20 8:43:28";
? ? //將strBirth字符串轉(zhuǎn)換成日期對象
? ? SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
? ? Date birth = sdf.parse(strBirth);? ?
? ? s.setBirth(birth);
? ? s.setSsex("女");
? ? s.setScls("J190802");
? ? request.setAttribute("stu", s);
? ? request.setAttribute("br", strBirth);
? ??
? ? Date now = new Date();
? ? //將日期對象轉(zhuǎn)換成字符串
? ? String str = sdf.format(now);
? ? System.out.println(str);
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? ? <head>
? ? ? ? <base hreff="<%=basePath%>">
? ? ? ? <title></title>
? ? ? ? <meta http-equiv="pragma" content="no-cache">
? ? ? ? <meta http-equiv="cache-control" content="no-cache">
? ? ? ? <meta http-equiv="expires" content="0">
? ? ? ? <meta http-equiv="keywords" content=
? ? ? ? "keyword1,keyword2,keyword3">
? ? ? ? <meta http-equiv="description" content="This is my page">
? ? </head>
? ? <body>
? ? ? ? <label>姓名:</label><h1>${stu.sname }</h1>
? ? ? ? <label>生日:</label><h1>${br }</h1>
? ? </body>
</html>

