第三章 JSP技术基础 实训三

today-date.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.time.LocalDate" %>

<!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>显示日期</title>
<%
	LocalDate today  = LocalDate.now();
%>
</head>
<body>
今天的日期是:<%=today %>

</body>
</html>

 

main.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>新世纪在线书店</title>
</head>
<body>
<%@ include file = "header.jsp" %>
<%@ include file = "body.jsp" %>
<%@ include file = "footer.jsp" %>

</body>
</html>

header.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>Insert title here</title>
</head>
<body>
<p style = "color:#ff0000;">
新世纪 网上书店</p>
<hr />
</body>
</html>

body.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>Insert title here</title>
</head>
<body>
<table border=0 cellspacing=5 cellpadding=5 width="100%">
	<tr><td>
	<p align="center"><b>欢迎光临新世纪网上书店!</b></p>
	</td>
	</tr>
	<tr><td>
	<p align="center"><b><a href="/bookstore/catalog">开始购买图书</a></b></p>
	</td>
	</tr>
</table>

</body>
</html>

footer.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>Insert title here</title>
</head>
<body>
<hr />
<center>版权所有 &copy; 2018 新世纪网上书店, Inc.</center>


</body>
</html>

 

inputProduct.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>输入商品信息</title>
</head>
<body>
<h4>输入商品信息</h4>
<form action="product-servlet"method = "post"><!-- 将请求转到ProductServlet -->
	<label>商品号:<input type="text"name="id"></label><br>
	<label>商品名:<input type="text"name="name"></label><br>
	<label>价格:<input type="text"name="price"></label><br>
	<label><input type="submit"value="确定">
		<input type="reset"value="重置"></label>
</form>
</body>
</html>

display-product.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<jsp:useBean id="product" class="com.demo.Product" scope="request"/>
<!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>商品信息</title>
</head>
<body>
	<table border = "1">
	<caption>商品信息如下</caption>
		<!-- 使用<jsp:getPropetty>动作输出信息 -->
	<tr>
	<td>商品号:</td>
	<td><jsp:getProperty property="id" name="product"/></td>
	</tr>
	<tr>
	<td>商品名:</td>
	<td><jsp:getProperty property="name" name="product"/></td>
	</tr>
	<tr>
	<td>价格:</td>
	<td><jsp:getProperty property="price" name="product"/></td>
	</tr>
	</table>
	
</body>
</html>

Product.java

package com.demo;

public class Product {
	private String id;//商品号
	private String name;//商品名
	private double price;//价格
	public Product() {
		super();
	}
	public Product(String id,String name,double price) {
		super();
		this.id = id;
		this.name = name;
		this.price = price;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return id;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	

}

productServlet.java

package com.demo;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ProductServlet
 */
@WebServlet(name = "productservlet", urlPatterns = { "/product-servlet" })
public class ProductServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProductServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, 
			HttpServletResponse response) 
			throws ServletException, IOException {
		
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		
		//修改请求参数的字符编码
		name = new String (name.getBytes("iso-8859-1"),"UTF-8");
		double price = Double.parseDouble(request.getParameter("price"));
		//创建一个product对象,然后将请求转到DisplayProduct.jsp
		Product product = new Product(id,name,price);
		request.setAttribute("product", product);
		RequestDispatcher rd = 
				request.getRequestDispatcher("/display-product.jsp");
		rd.forward(request, response);
		
		
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值