#1
查询页
enquire.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>
<form action="/javawebtest1/enquire" method="post">
<table>
<tr>
<td>电费余额查询:</td><td><input type="submit" value="查询"></td>
</tr>
</table>
</form>
</body>
</html>
#2
enquireServlet.java
package com.neu.server;
import java.io.IOException;
import java.util.Random;
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 enquireServlet
*/
@WebServlet("/enquire")
public class enquireServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Random r=new Random();
int n=r.nextInt(200);
response.sendRedirect(request.getContextPath()+"/show.jsp?e="+n);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
#3
显示页面
show.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>
<%
int n=Integer.parseInt(request.getParameter("e"));
String m="";
if(n<100){
m="余额不足,请及时充值!";
}else{
m="余额充足,余额为"+n;
}
%>
<%=m %>
</body>
</html>