@webservlet注解案例




import com.example.demo1.HelloServlet;

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;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@WebServlet(value = "/Calculate")
public class Calculate extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List li =new ArrayList();
String str =req.getParameter( "r" );//获取半径
li = circle( str );
double perimeter=(double) li.get( 0 );//周长
double area=(double) li.get( 1 );//面积
resp.setContentType("text/html;charset=UTF-8");
PrintWriter writer =resp.getWriter();
writer.write( "周长是:"+perimeter+"面积是:"+area);
}


/**
* 计算面积
* @param str 半径
* @return 返回面积值
*/
public List circle(String str){
List list =new ArrayList();
double p = 0;//周长
double a = 0;//面积
String r =str;
if (r==null||r.equals( "" )){
System.out.println("输入的值异常");
}else {
p = Double.parseDouble(str)*2*Math.PI;
a = Double.parseDouble( str )*Double.parseDouble( str )*Math.PI;
}
list.add( p);
list.add( a );
return list;
}

}

---------------------------------------------------------------------
<%--
Created by IntelliJ IDEA.
User: zhangjiaming
Date: 2021/11/17
Time: 10:32
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="Calculate" name="form" method="get">
<input type="text" name="r" value="0">
<input type="submit" name="submit" value="运算">
</form>

</body>
</html>
上一篇:数组的对象元素,按照对象的某个键分类,把键的值相同的放到一个数组里,生成一个新的二维数组


下一篇:Java 文件下载案例