Java web:JSP,登录,创建商品类,行为标签指令在页面上显示赋值的内容【诗书画唱】
1、创建一个商品类,包含编号,名称和价格,通过jsp的行为标签(就是用创建一个商品类,并给他的所有属性赋值。最后在页面上显示赋值的内容。

package com.SSHC;
public class ShangPin {
private String bianHao;
private String name;
private Double price;
public String getBianHao() {
return bianHao;
}
public void setBianHao(String bianHao) {
this.bianHao = bianHao;
}
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;
}
}


<%@ 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>

<title>诗书画唱提醒你!跟我学JSP行为!</title>
<!-- 下面的meta在这里删掉都没太多影响 -->
<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>
<!-- 下面的是给ShangPin取别名为s。
Bean是描述Java的软件组件模型 -->
<jsp:useBean id="s" class="com.SSHC.ShangPin"></jsp:useBean>
<!-- property="属性名" name="类的别名" value="自己设置
和赋值的属性值。自己的翻译:setProperty为设置配置"。
property:特性;性质;性能;-->
<jsp:setProperty property="bianHao" name="s" value="666"/>
<jsp:setProperty property="name" name="s" value="诗书画唱CD"/>
<jsp:setProperty property="price" name="s" value="6666.66"/>
<jsp:getProperty property="bianHao" name="s"/>
<jsp:getProperty property="name" name="s"/>
<jsp:getProperty property="price" name="s"/>
</body>
</html>


2、创建一个注册页面reg.jsp,包含账号,密码,性别(单选框),爱好(多选框)和学历(下拉框),生日(日期框),通过表单提交方式提交到doReg.jsp,获取表单中的所有数据并打印出来,注意中文乱码处理。


<%@ 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>

<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">
<style type="text/css">
*{
font-size:23px;
}
</style>
</head>
<body>
<h1>注册页面</h1>
<form action="doReg.jsp" method="post">
<table border="1" >
<tr>
<td>账号:</td>
<td><input type="text" name="zhangHao" /></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="pwd" /></td>
</tr>
<tr>
<td>确认密码:</td>
<td><input type="password" name="pwdSure" /></td>
</tr>
<tr>
<td>生日</td>
<td>
<!-- type="date" 是HTML5中才有很好效果的
代码,在这里和 type="text"没有太多的区别-->
<input type="date" name="brithday" />
</td>
</tr>
<tr>
<td>学历</td>
<td>
<select name="xueLi">
<option value="">请选择学历</option>
<option value="1">本科</option>
<option value="2">大专</option>
<option value="3">高中</option>
<option value="4">初中</option>
</select>
</td>
</tr>
<tr>
<td>性别:</td>
<td>
<input type="radio" name="sex"
value="男" checked />男
<input type="radio" name="sex"
value="女" />女
</td>
</tr>
<tr>
<td>爱好:</td>
<td>
<input type="checkbox" name="aiHao" value="唱歌和给诗书画唱三连"
checked/>唱歌和给诗书画唱三连
<input type="checkbox" name="aiHao"
value="跳舞和给诗书画唱关注" />跳舞和给诗书画唱关注
<input type="checkbox" name="aiHao" value="看书和诗书画唱的视频" />
看书和诗书画唱的视频
<input type="checkbox" name="aiHao" value="rap" />rap
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="reset" value="重置" />
<input type="submit" value="提交" />
</td>
</tr>
</table>
</form>
</body>
</html>










3、创建一个商品新增页面add.jsp,要求输入名称和价格,通过表单提交到doAdd.jsp页面,将获取到的商品名称和价格封装到商品对象中(可以复用第1题中的商品类)


package com.SSHC;
public class ShangPin {
private String bianHao;
private String name;
private Double price;
public String getBianHao() {
return bianHao;
}
public void setBianHao(String bianHao) {
this.bianHao = bianHao;
}
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;
}
}


<%@ 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>

<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">
<style type="text/css">
*{
font-size:23px;
}
</style>
</head>
<body>
<h1>商品界面</h1>
<form action="doAdd.jsp" method="post">
<table border="1" >
<tr>
<td>商品名称:</td>
<td><input type="text" name="spName" /></td>
</tr>
<tr>
<td>商品价格:</td>
<td><input type="text" name="spPrice" /></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="reset" value="重置" />
<input type="submit" value="提交" />
</td>
</tr>
</table>
</form>
</body>
</html>



<%@ 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>

<title>诗书画唱提醒你!跟我学JSP行为!</title>
<!-- 下面的meta在这里删掉都没太多影响 -->
<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>
<%
//下面是用request处理中文乱码:
request.setCharacterEncoding("utf-8");
String spName= request.getParameter("spName");
System.out.println("商品名称:"+spName);
String spPrice= request.getParameter("spPrice");
System.out.println("商品价格:"+spPrice);
/**值得注意的是这里要把String类型转换成Double类型等,因为在自己写的
shangPin类中,Price为Double类型,在用下面的
JSP动作指令插入Double类型内容中时,不这样的话,就会报错等*/
Double spPriceDouble=Double.valueOf(spPrice);
%>
<!-- 下面的是给ShangPin取别名为s。
Bean是描述Java的软件组件模型 -->
<jsp:useBean id="s" class="com.SSHC.ShangPin"></jsp:useBean>
<!-- property="属性名" name="类的别名" value="自己设置
和赋值的属性值。自己的翻译:setProperty为设置配置"。
property:特性;性质;性能;-->
<jsp:setProperty property="name" name="s" value="<%=spName%>"/>
<jsp:setProperty property="price" name="s"
value="<%=spPriceDouble%>"/>
<!-- 下面是得到内容,之后可以打印,显示到网页上 -->
<jsp:getProperty property="name" name="s"/>
<jsp:getProperty property="price" name="s"/>
</body>
</html>



推荐文章:
Bean是描述Java的软件组件模型
http-equiv顾名思义,相当于http的文件头作用,
它可以向浏览器传回一些有用的信息,以帮助正确和精确地显示网页内容,
与之对应的属性值为content,
content中的内容其实就是各个参数的变量值。
https://www.cnblogs.com/dreamaker/p/10576750.html


推荐网站:
https://developer.aliyun.com/?spm=a2c6h.12873639.fszjobuve.3.4eb74ea4irbQAb


