用doGet来接收客户的请求

呵呵,在MyEclipse里面新建一个项目gagaj2ee002,在webroot中新建一额html的静态页面,用来发送数据,新建一个servlet来处理html页面发来的内容。

html中的内容是:

——————————————————————-

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”>
<html>
<head>
<title>index.html</title>

<meta http-equiv=”keywords” content=”keyword1,keyword2,keyword3″>
<meta http-equiv=”description” content=”this is my page”>
<meta http-equiv=”content-type” content=”text/html; charset=UTF-8″>

</head>

<body>
<form action=”testDoGet.do” method=”get”>
<input type=”text” name=”name”/><br/>
<input type=”submit” value=”发送”/>
</form>
</body>
< /html>

—————————————————————————

servlet的内容是:

package ynu.sanwen.gagaj2ee;

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 TestDoGet extends HttpServlet {

private static final long serialVersionUID = 8458149824838390025L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType(“text/html”);
PrintWriter out = response.getWriter();
out
.println(“<!DOCTYPE HTML PUBLIC \”-//W3C//DTD HTML 4.01 Transitional//EN\”>”);
out.println(“<HTML>”);
out.println(“ <HEAD><TITLE>A Servlet</TITLE></HEAD>”);
out.println(“ <BODY>”);
out.println(“this id doget”);
out.println(“ </BODY>”);
out.println(“</HTML>”);
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

this.doGet(request, response);
}

}

查看效果:

共有 13 条评论

Top