消息关闭
    暂无新消息!
public class LoginServlet extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html;charset=utf-8");
// response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();

//返回一个界面(html技术)
out.println("<h1>用户登录</h1>");
out.println("<form action='/UsersManager/LoginClServlet'  method='post'>");
out.println("用户名:<input type='text' name='username'/><br>");
out.println("密  码:<input type='password' name='password'/><br>");
out.println("<input type='submit' value='登陆'/><br>");
out.println("</form>");


跳转页面 如下

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();

//接收用户提交的用户名和密码
    String username=request.getParameter("username");
String password=request.getParameter("password");



//看看没有接受到数据 
System.out.println(username+" "+password);



web.xml 如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>com.hsp.view.LoginServlet</servlet-class>
  </servlet>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>LoginClServlet</servlet-name>
    <servlet-class>com.hsp.controller.LoginClServlet</servlet-class>
  </servlet>




  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/LoginServlet</url-pattern>
  </servlet-mapping>
 
  <servlet-mapping>
    <servlet-name>LoginClServlet</servlet-name>
    <url-pattern>/LoginClServlet</url-pattern>
  </servlet-mapping>
 

</web-app>;

3个回答

︿ 0
out.println("<form action='/UsersManager/LoginClServlet'  method='post'>");

你这里肯定接收不到post请求了,你的方法执行的是get请求,
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException{
   doGet(request, response);
}

看你的容器应是2.5以上,不用在web.xml做配置,直接采用注解


@WebServlet(UrlPatterns="/servlet/login.do")
public class LoginServlet{
     public void doGet(){
  }
}