程序小白,希望和大家多交流,共同学习
package demo.controller;
import demo.service.Service;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ComputeServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
//1.接收圆的半径
String r = "";
r = (String)request.getParameter("radius");
if (r.equals("") || r == null)
{
//System.out.println("null");
response.sendRedirect("index.jsp");//未输入圆的半径,返回输入页面
}
else
{
double radius = Double.parseDouble(r);
//2.计算圆的面积
Service service = new Service();
double area = service.computeArea(radius);
String msg = "半径为:" + radius + "圆的面积为:" + area + "!";
HttpSession session = request.getSession(true);//获取会话
session.setAttribute("msg", msg);
response.sendRedirect("showArea.jsp");
}
}
}
//服务层
package demo.service;
public class Service
{
public double computeArea(double radius)
{
return Math.PI * radius * radius;
}
}
<%@ page language="java" contentType="text/html; charset=GB2312" %>
<html>
<head>
<title> Servlet版本计算圆的面积</title>
<style type="text/css">
form, h1{
text-align:center;
color:#33ffcc;
}
</style>
</head>
<body>
<h1> Servlet版本的计算圆的面积</h1>
<form action="compute.action" method="post" onsubmit="return check()">
请输入圆的半径:<input type="text" name="radius"/>
<input type="submit" valur="计算"/>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=GB2312" %>
<html>
<head>
<title>Servlet版本的计算圆的面积</title>
<style type="text/css">
body{
text-align:center;
color:#00ffcc;
}
</style>
</head>
<body>
<h1> Servlet版本的计算圆的面积</h1>
<hr />
<br />
<%= (String)session.getAttribute("msg")%>
<a href="index.jsp"> 返回继续 </a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=GB2312" %>
<html>
<head>
<title>MVC模式的注册登录系统</title>
<style type="text/css">
body{
text-align:center;
color:#0066ff;
}
</style>
</head>
<body>
<h1>MVC模式的注册登录系统</h1>
<hr />
<br />
<%= (String)session.getAttribute("msg")%>
<a href="index.jsp">返回继续</a>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0" metadata-complete="true">
<servlet>
<servlet-name>computearea</servlet-name>
<servlet-class>demo.controller.ComputeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>computearea</servlet-name>
<url-pattern>/compute.action</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>