【实验要求】
(1)掌握Servlet的原理;
(2)会编写Servlet程序;
(3)掌握MVC的工作原理。
【实验目的】
(1)会用Servlet处理程序;
(2)能明确定位M-JavaBean,V-Servlet,C-JSP。
【实验环境】
jdk+eclipse+tomcat
【实验内容】
用MVC模式求等差数列和
(1)设计input.jsp页面,用户可以输入等差数列首项、项数和等差;
(2)设计Bean,即Series.java文件,有属性首项、项数和等差以及seter和getter;
(3)在Bean中添加求和方法getSum();
(4)编写InputServlet响应input.jsp中表单请求;
(5)在result.jsp中显示结果。
【实验代码】
inputData.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>inputData.jsp</title>
</head>
<body>
<FORM action="computer" Method="post">等差数列求和: <BR>
输入首项:<Input type=text name="firstItem" size=4> 输入公差:<Input type=text name="var" size=4> 求和项数:<Input
type=text name="number" size=4> <Input type=submit value="提交">
</FORM>
</body>
</html>
showResult.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="lie" class="ghjghfgh.dengbean" scope="request" />
<Table border=1>
<tr>
<th>数列的首项</th>
<th><jsp:getProperty name="lie" property="name" /></th>
<th>所求项数</th>
<th>求和结果</th>
</tr>
<tr>
<td><jsp:getProperty name="lie" property="firstItem" /></td>
<td><jsp:getProperty name="lie" property="var" /></td>
<td><jsp:getProperty name="lie" property="number" /></td>
<td><jsp:getProperty name="lie" property="sum" /></td>
</tr>
</Table>
</body>
</html>
dengbean.java
package ghjghfgh;
public class dengbean {
double firstItem; //数列首项
double var; //公差
int number; //求和项数
double sum; //求和结果
String name = ""; //数列类别
public void setFirstItem(double a) {
firstItem = a;
}
public double getFirstItem() {
return firstItem;
}
public void setVar(double b) {
var = b;
}
public double getVar() {
return var;
}
public void setNumber(int n) {
number = n;
}
public double getNumber() {
return number;
}
public void setSum(double s) {
sum = s;
}
public double getSum() {
return sum;
}
public void setName(String na) {
name = na;
}
public String getName() {
return name;
}
}
InputServlet.java
package ghjghfgh;
import java.io.IOException;
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 InputServlet
*/
@WebServlet("/InputServlet")
public class InputServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public InputServlet() {
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 {
// TODO Auto-generated method stub
doGet(request, response);
}
}
dengservlet.java
package ghjghfgh;
import ghjghfgh.dengbean;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/computer")
public class dengservlet extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
dengbean shulie = new dengbean(); //创建Javabean对象
request.setAttribute("lie", shulie);//将shulie存储到HttpServletRequest对象中
double a = Double.parseDouble(request.getParameter("firstItem"));
double d = Double.parseDouble(request.getParameter("var"));
int n = Integer.parseInt(request.getParameter("number"));
shulie.setFirstItem(a); //将数据存储在shulie中
shulie.setVar(d);
shulie.setNumber(n);
//计算和:
double sum = 0, item = a;
int i = 1;
shulie.setName("等差数列的公差");
while (i <= n) //计算等差数列的和
{
sum = sum + item;
i++;
item = item + d;
}
shulie.setSum(sum);
RequestDispatcher dispatcher = request.getRequestDispatcher("showResult.jsp");
dispatcher.forward(request, response); //请求showResult.jsp显示shulie中的数据
}
}
【实验结果】