Login.jsp
<body>
<form action="ServletTest" method="post">
<table align="center">
<tr><td>name:<td><td><input type="text" name="name"><td><tr>
<tr><td>psw:<td><td><input type="text" name="psw"><td><tr>
<tr><td><td><td><input type="submit" value="提交"><td><tr>
</table>
</form>
</body>
通过index.jsp 用forward专向Login.jsp。
<jsp:forward page="/WEB-INF/Login.jsp"></jsp:forward>
在项目中创建一个servlet 类,继承HttpServlet,重写doGet(),doPost()
ServletTest.java
package com.scx.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 ServletTest extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//req.setCharacterEncoding("utf-8"); 这种转换会乱码
resp.setContentType("text/html;charset=UTF-8");
String name = req.getParameter("name");
String psw = req.getParameter("psw");
PrintWriter out = resp.getWriter();
if(name=="123"||name.equals("123")){
if(psw=="123"||psw.equals("123")){
out.println("登录成功,"+"欢迎"+name+"进入我的空间!");
}
}
else{
out.println("用户名或密码错误!");
}
}
}
过程中出现了 HTTP method GET is not supported by this URL 这种错误,网上搜了好久,原来是我是用的POST传值,结果在ServletTest中,写到了doGet()方法体内,真是太晕了,就先到这吧,晚安,地球。