J2EE之Servlet学习笔记(二):获取提交的参数与返回响应

学习资料:http://how2j.cn/k/servlet/servlet-paramter/547.html

                    http://how2j.cn/k/servlet/servlet-response/548.html

获取参数

使Servlet获取从浏览器提交的参数

1.创建login.html(前端部分)
在上一节的web目录下,创建login.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<body>
  
<form action="login" method="post">
账号: <input type="text" name="name"> <br>
密码: <input type="password" name="password"> <br>
<input type="submit" value="登录">
</form>
  
</body>
</html>

这里包含一个form元素,其中
action="login" 表示会提交到login路径login路径在后续步骤中会映射到LoginServlet
method="post" 表示提交的密码信息在浏览器地址栏不显示
账号部分使用普通的文本输入框 命名name="name“”,密码部分使用密码输入框 命名name"password"

2.创建LoginServlet(后端部分)

import java.io.IOException;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
  
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String name = request.getParameter("name");
        String password = request.getParameter("password");
  
        System.out.println("name:" + name);
        System.out.println("password:" + password);
    }
}

与前端html中的method="post”对应,这里需要提供doPost()方法
方法体中,通过request.getParameter()方法,以前端中账号和密码的name为参数,分别获取前端传递的数据

3.映射login路径到LoginServlet
此时虽然分别完成了前端和后端,也准备好了对接,但他们之间的桥梁还没有建立。
如果这时候启动tomcat,已经可以访问login.html,但点击提交后不会有任何反应,所以映射就是完成这一步搭桥

编辑WEB-INF中的web.xml
新增映射

    <servlet>
        <servlet-name>LoginServlet</servlet-name>
        <servlet-class>LoginServlet</servlet-class>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>LoginServlet</servlet-name>
        <url-pattern>/login</url-pattern>
    </servlet-mapping>  

保存后重启tomcat,访问http://127.0.0.1/login.html
输入账号密码后提交,可以看到浏览器路径变为了http://127.0.0.1/login
并且可以在tomcat窗口中看到提交的数据

这里要提一下,如果不通过login.html提交数据,而直接访问http://127.0.0.1/login会报错
这是因为直接访问login路径是doGet()的方式,而在LoginServlet中只写了doPost()没有doGet()

===================================================================================================

返回响应

返回HTML响应给浏览器

根据浏览器提交的账号密码,返回登录成功或者失败给浏览器
简化设定:如果用户名为admin,密码为123,即登录成功,否则失败

修改LoginServlet

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 LoginServlet extends HttpServlet {
  
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
  
        String name = request.getParameter("name");
        String password = request.getParameter("password");
  
        String html = null;
  
        if ("admin".equals(name) && "123".equals(password))
            html = "<div style='color:green'>success</div>";
        else
            html = "<div style='color:red'>fail</div>";
  
        PrintWriter pw = response.getWriter();
        pw.println(html);
  
    }
  
}

这里的细节是,写比较逻辑的时候把常量字符串"admin"和"123"放在前面,规避了因提交空数据导致的空指针异常

重启tomcat后访问login.html进行测试

猜你喜欢

转载自blog.csdn.net/HiflyMaple/article/details/87185259