前端与后台之间的数据传递(2)

jsp与后台之间数据的传递

jsp与后台之间数据的传递,可以说在一定程度上解决了前端与后台数据互相传递的问题,但如果将jsphtml代码嵌套在一起,会造成可读性差等问题。

下面也通过一个登录案例来讲解jspweb之间数据的传递。

1)首先建立一个用于登录的login.jsp页面,具体实现代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>登录界面</title>

<style>

body{width:300px; height:200px; margin:0 auto;}

fieldset{ width:300px; }

table{ width:300px; height:100px;}

</style>

</head>

<body>

<fieldset>

<legend>登录界面</legend>

<form action="../LoginServlet" method="post">

<table>

<tr><td>姓名:</td><td><input type="text" name="name"/></td></tr>

<tr><td>密码:</td><td><input type="text" name="password"/></td></tr>

</table>

<input type="submit" value="提交" style="margin-left:66px; width:70px;"> 

<input type="reset" value="重置" style="width:70px;"/>

</form>

</fieldset>

</body>

</html>

 

2)建立一个用于接收从前台传递过来参数的ServletLoginSservlet.java,代码如下:

package zjh.javaee.servlet;

 

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

 

@WebServlet("/LoginServlet")

public class LoginServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    public LoginServlet() {

        super();

    }

 

 

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

this.doPost(request, response);

}

 

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//解决乱码问题

request.setCharacterEncoding("UTF-8");

response.setCharacterEncoding("UTF-8");

//接收请求的参数

String userName = request.getParameter("name");

String userPassword = request.getParameter("password");

System.out.println("姓名为:"+userName+":"+"密码为:"+userPassword);

//判断输入的姓名、密码是否为空,如果不为空则跳转到显示结果的show.html页面

if(userName.equals("")||userName==null||userPassword.equals("")||userPassword==null){

System.out.println("error");

}else{

request.setAttribute("name",userName); //将数据封装在name里,然后将其传递到show.jsp

//服务器端跳转

request.getRequestDispatcher("/jsp/show.jsp").forward(request, response);

}

}

 

}

 

3)建立一个用于显示登录结果的show.jsp页面,代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>显示界面</title>

</head>

<body>

欢迎你,<%=request.getAttribute("name") %><!-- 用于接收从后台传过来的数据 -->

</body>

</html>

从上面两种方法不难看出,要想进行前端与后台之间数据的传递,必须通过表单并且需要为action指定目标位置,最后都需要通过<input type=”submit”>这个提交按钮来进行提交,这就在很大程度上限制了程度的灵活性,所以了为了解决这种问题,在接下来的前端与后台数据之间的传递(3)中使用jQuery中的$.post()方法来进行讲解。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值