首先创建一个html文件,form表单中action为servlet的url值,由于定义文件名为a,所以我在这里的action="a",input里面定义两个类型,一个文本框,一个提交按钮,然后再将文本框里面数据定义name,方便传值。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="a" >
<input type="text" name="data">
<input type="submit">
</form>
</body>
</html>

  然后创建一个servlet文件:

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class a
 */
@WebServlet("/a")
public class a extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public a() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        int a=Integer.parseInt(request.getParameter("data"));   //获得html表单中的数据,定义int型
        request.setAttribute("data", a);//传值调用
        for(int i=2;i<a;i++) { 
        if(a%i==0) {
        break;
        }
        if(a%i!=0&&i==a-1) {
        request.setAttribute("shuju", i);
        }
        }//确认a是否为素数
        
        request.getRequestDispatcher("servlet111").forward(request, response);

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

再创建一个servlet:

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class servlet111
 */
@WebServlet("/servlet111")
public class servlet111 extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public servlet111() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        if(request.getAttribute("shuju")==null) {//获得数据
        response.getWriter().println(request.getAttribute("data")+"不是素数"+"</br>");
        }
        else {
        response.getWriter().println(request.getAttribute("data")+"是素数"+"</br>");
        }

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

在servlet的传值中,运用了两个,其中传值的是request.setAttribute("****",***)
     接受的是request.getAttribute("****")

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐