其他分享
首页 > 其他分享> > @webservlet注解案例

@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>

标签:double,webservlet,案例,str,import,注解,li,servlet,javax
来源: https://www.cnblogs.com/nbfangzhu/p/15575658.html