首先在JSP页面中输入以下代码:<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
然后创建Servlet:import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Action extends HttpServlet { //继承自类javax.servlet.http.HttpServlet
private static final long serialVersionUID = 1L; /**
* 接受处理post方式的方法
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { //重写doPost方法 response.setContentType("text/html");
PrintWriter out = response.getWriter();
int i = Integer.valueOf(request.getParameter("shuzi")); //从页面获取传入的值
double j = Math.pow(i, 0.5); //进行平方根操作
out.print(j); //在页面上答应处理后的结果
}
}
编写web.xml配置文档,在web.xml文档中加入如下代码:
Action
Action
Action
/action