您的位置:首页 > 其它

一个Servlet的实例

2016-04-23 21:17 239 查看
需求:在一个表单中输入姓名和年龄,点击提交,然后打印出来。

1.新建一个WebProject.

2.在WebRoot目录下新建一个html文件(这里取名为myHtml.html).



<!DOCTYPE html>
<html>
<head>
<title>登录</title>
</head>

<body>
<form action="loginServlet" method="get">
姓名:<input type="text" name="name" style="width:150px" /><p/>
年龄:<input type="text" name="age" style="width:150px" /><p>

<input type="submit" value="提交" />
</form>
</body>
</html>


3.在src 目录上点击右键,新建一个Servlet。Servelt 名为LoginServlet.包名为myServletTest.

4.编辑WebRoot中的Web.xml 文件。


5.编辑LoginServlet.java文件。

package myServletTest;

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

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @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 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.print("    This is ");
//      out.print(this.getClass());
//      out.println(", using the GET method");
//      out.println("  </BODY>");
//      out.println("</HTML>");
//      out.flush();
//      out.close();
String name = request.getParameter("name");
String age = request.getParameter("age");
PrintWriter out = response.getWriter();
out.println("name =" + name);
out.println("age=" + age);
out.flush();
out.close();
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to 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 {

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.print("    This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println("  </BODY>");
out.println("</HTML>");
out.flush();
out.close();

}

}


6.启动服务器。

7.访问



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: