能不能通过导入头文件或者是其他方法都不写这两种方法还是可以传递信息?(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);
}
}
}
因为你的LoginServlet .java文件的doGet()和doPost()会自动识别 你jsp页面上,<a href=".............................">或者表单<form method="post/get"> 所提交的方式. <a href="......"> 默认以doGet方式获取 <form 一般以post方式提交》