欢迎光临散文网 会员登陆 & 注册

JSP作业和例子:map,put,EL表达式,下拉框selected,单选框checked【诗书画唱】

2020-09-09 22:57 作者:诗书画唱  | 我要投稿

1、创建一个商品类,包含商品名称和商品价格两个属性,在a.jsp页面创建一个<String,Product>类型的map,添加三样商品,跳转到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、创建一个Person类,包含姓名,性别,学历三个属性,在b.jsp页面创建一个Person对象,跳转到result.jsp页面后通过EL表达式显示姓名,性别和学历(注意:学历是一个下拉框)

<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>学历:</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、创建一个Cat类,包含名字和毛色,在c.jsp创建一个Cat对象,跳转到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>




——————————————————————


例子(含自己写的题目内容):

声明Map<String,String>map, Student 类,把内容放进去,之后用EL表达式打印出来(request.setAttribute("fruitMap", map);, map.put("4号", "石榴");

中的"fruitMap"就像钥匙,用${fruitMap["4号"] }“打开”(打印)"石榴"(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"的内容






<%@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字符串转换成日期对象

    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");

    //跳转到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>



用上页面跳转,要求把改为String  sex(这里用char的话容易报错)的Student的内容跳转并打印到别的界面,性别用单选框显示:





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("当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显示日期到网页:





<%@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字符串转换成日期对象

    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();

    //将日期对象转换成字符串

    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>



JSP作业和例子:map,put,EL表达式,下拉框selected,单选框checked【诗书画唱】的评论 (共 条)

分享到微博请遵守国家法律