servlet必须要写Doget或Dopost方法吗?
能不能通过导入头文件或者是其他方法都不写这两种方法还是可以传递信息?(JSP与servlet相关问题!)
package lee;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.PrintWriter;
import java.io.IOException;
import java.sql.*;
/*@WebServlet(name="login"
, urlPatterns={"/login"})*/
public class LoginServlet extends HttpServlet
{
public void service(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,java.io.IOException
{
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charSet=utf-8");
String errMsg = "";
RequestDispatcher rd;
String username = request.getParameter("username");
String pass = request.getParameter("pass");
try
{
if (admin.equals(pass))
{
HttpSession session = request.getSession(true);
״̬
session.setAttribute("name" , username);
rd = request.getRequestDispatcher("/welcome.jsp");
rd.forward(request,response);
}
else
{
errMsg += "你的用户名密码不符合,请重新输入";
}
}
catch (Exception e)
{
e.printStackTrace();
}
if (errMsg != null && !errMsg.equals(""))
{
rd = request.getRequestDispatcher("/login.jsp");
request.setAttribute("err" , errMsg);
rd.forward(request,response);
}
}
}