package com.mvc; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class example6_1 extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L; @Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
model data = new model();// 创建javabean对象 request.setAttribute("data", data);
String shouxiang = request.getParameter("a");
int a = Integer.parseInt(shouxiang);
data.setA(a);
String xiangshu = request.getParameter("n");
int n = Integer.parseInt(xiangshu);
data.setN(n);
String gong = request.getParameter("q");
int q = Integer.parseInt(gong);
data.setQ(q);
data.setName("等差数列的公差");
int sum = ;
for (int i = ; i < n; i++) {
sum += a;
a += q;
}
data.setsum(sum);
RequestDispatcher dispatcher = request.getRequestDispatcher("show.jsp");
dispatcher.forward(request, response); } @Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
model data = new model();// 创建javabean对象
request.setCharacterEncoding("iso-8859-1");
request.setAttribute("data", data);
String shouxiang = request.getParameter("a");
int a = Integer.parseInt(shouxiang);
data.setA(a);
String xiangshu = request.getParameter("n");
int n = Integer.parseInt(xiangshu);
data.setN(n);
String gong = request.getParameter("q");
int q = Integer.parseInt(gong);
data.setQ(q);
data.setName("等比数列的公差");
int sum = ;
for (int i = ; i < n; i++) {
sum += a;
a *= q;
}
data.setsum(sum);
RequestDispatcher dispatcher = request.getRequestDispatcher("show.jsp");
dispatcher.forward(request, response); }
} package com.mvc; public class model {
int a;
int n;
int q;
int sum;
String name = ""; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getsum() {
return sum;
} public void setsum(int sum) {
this.sum = sum;
} public int getA() {
return a;
} public void setA(int a) {
this.a = a;
} public int getN() {
return n;
} public void setN(int n) {
this.n = n;
} public int getQ() {
return q;
} public void setQ(int q) {
this.q = q;
} } <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>hhh</servlet-name>
<servlet-class>com.mvc.example6_1</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>hhh</servlet-name>
<url-pattern>/heh</url-pattern>
</servlet-mapping>
</web-app> <%@ page contentType="text/html; charset=gb2312"%> <html>
<head>
<title>这是一个动态页面</title>
</head>
<body>
<form action="show.jsp" method="get">
等差数列求和:<br> 输入首项<input type="text" name="a">
输入公差:<input type="text" name="q">
求和项数:<input type="text" name="n">
<input type="submit" value="提交">
</form> <form action="heh" method="post">
等比数列求和:<br> 输入首项<input type="text" name="a">
输入公比:<input type="text" name="q">
求和项数:<input type="text" name="n">
<input type="submit" value="提交">
</form> </body>
</html> <%@ page contentType="text/html; charset=gb2312"%>
<jsp:useBean id="data" class="com.mvc.model" scope="request"></jsp:useBean>
<table border="">
<tr>
<th>数列的首项</th>
<th><jsp:getProperty property="name" name="data" /></th>
<th>所求项数</th>
<th>求和结果</th>
</tr>
<tr>
<td><jsp:getProperty property="a" name="data" /></td>
<td><jsp:getProperty property="name" name="data" /></td>
<td><jsp:getProperty property="n" name="data" /></td>
<td><jsp:getProperty property="sum" name="data" /></td>
</tr>
</table>
一定要仔细检查每一个地方, 检查哪些最容易被忽略的地方。 例如包名是否与预留关键字冲突,web.xml位置是否正确。等