2 回答
TA贡献1824条经验 获得超8个赞
方法如下:
USERFORM.JSP
<%@ page language="java"
contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>用户表单</title>
</head>
<body>
<form
action="DoGetDemo" method="get">
用户名:<input type="text"
name="username"/><br>
密码:<input type="password"
name="password"/><br>
<input type="submit"
value="提交"/>
<input type="reset"
value="重置"/>
</form>
</body>
</html>
OK,接下来相对应的Servlet代码如下:
package com.javaweb.ch10;
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 DoGetDemo extends HttpServlet {
public
void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("gb2312");
String username = request.getParameter("username");
String password = request.getParameter("password");
out.println("<html>");
out.println("<body>");
out.println("用户名:" + username +
"<br>");
out.println("密码:" + password);
out.println("</body>");
out.println("</html>");
}
}
TA贡献1877条经验 获得超6个赞
快捷键不知道有没有,不过我一般这样弄也挺快的,一般我们写servlet时让那个类继承自HttpServlet,ctrl+s保存一下,然后选中你的类名,右击鼠标,然后点Override/Implment Methods,选择doGet点OK就有了。
- 2 回答
- 0 关注
- 1715 浏览
添加回答
举报