码迷,mamicode.com
首页 > Web开发 > 详细

JSP/Servlet Web 学习笔记 DayFour —— 实现一个简单的JSP/Servlet交互

时间:2017-06-05 23:32:36      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:dmi   serer   rpd   ddt   cdt   md5   zll   ctr   mapr   

小实例说明:

    a)实现一个由JSP负责前台显示,Servlet负责后台处理的交互小实例

    b)JSP页面由表单获取一个开始数字,一个结束数字,交给Servlet打印响应的乘法表。

未解决的问题:

    a)跳转貌似不能识别目录中的HTML文件,所以我把源码复制到JSP中,结果能找到。

    b)Servlet获取到JSP传过来的数据后,如果想在JSP页面out.print相关的JS代码貌似不能实现

      目前的效果是:在当前的Servlet页面弹出了JS alert()弹窗

             再重定向回之前的JSP页面。

源码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<style>
        h1{margin:0 auto;
       text-align: center;}
        h3{margin:0 auto;
       text-align: center;
        margin-bottom: 40px;}
        .contair{margin:0 auto;
                 background-color:yellow;
         width: 400px;
         padding: 20px 40px;}
</style>

<body>
<% 
    Boolean islarger=false;
    Boolean iserror=false;
    islarger=(Boolean)request.getAttribute("Larger");
    if(islarger!=null){
        if(islarger==true){
               out.println("<Script>");
               String message="初始数字大于结束数字!";
               out.println("alert(‘"+message+"‘)");
               out.println("</Script>");
        }
    }
    
    
    iserror=(Boolean)request.getAttribute("error");
    if(iserror!=null){
        if(iserror==true){
               out.println("<Script>");
               String message="输入的内容有误!";
               out.println("alert(‘"+message+"‘)");
               out.println("</Script>");
        }
    }
    
    


%>
<h1>Html与Servlet设计模式实例1</h1>
<hr>
<div class="contair">
    <h3>打印乘法表</h3>
    <form method="post" action="FS">
            初始数字:<input type="text" name="begin"><br>
            结束数字:<input type="text" name="end"><br>
            <hr>
            <input type="submit" value="提交">
            <input type="reset"  value="清空">
    </form>

</div>

</body>        
</html>
package firstServlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.jasper.tagplugins.jstl.core.Out;


@WebServlet("/FS")
public class FS extends HttpServlet {
    private static final long serialVersionUID = 1L;
    String message;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FS() {
        super();
        message="helloworld";
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();
        
        String Start=request.getParameter("begin");
        String End=request.getParameter("end");
        try {
            int SN=Integer.parseInt(Start);
            int EN=Integer.parseInt(End);
            if(SN>EN){
                   boolean isLarger=true;
                   request.setAttribute("Larger", isLarger);
                   request.getRequestDispatcher("servletcom.jsp").forward(request, response);
                  
                   
                   
            }
            else{
                request.setAttribute("SN", SN);
                request.setAttribute("EN", EN);
                RequestDispatcher dispatcher=request.getRequestDispatcher("Result");
                dispatcher.forward(request, response);
            }
            
            
            
            
        } catch (Exception e) {
            // TODO: handle exception
            boolean isError=true;
            request.setAttribute("error", isError);
            request.getRequestDispatcher("servletcom.jsp").forward(request, response);
        }
        
        
    }

}
package firstServlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Result
 */
@WebServlet("/Result")
public class Result extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Result() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();
        out.println("<h1>运算结果</h1>");
        int SN=(int)request.getAttribute("SN");
        int EN=(int)request.getAttribute("EN");
        out.println("<hr>");
        out.print("起始数字:"+SN+"   终止数字:"+EN);
        out.println("<hr>");
        for(int i=SN;i<=EN;i++){
            for(int j=SN;j<=i;j++)
            {
                out.print(i);
                out.print("*");
                out.print(j);
                out.print("=");
                out.print(i*j);
                out.print("         ");
            }
            out.print("<br>");
            
        }
    }

}

效果实例:

技术分享

技术分享

 

alert()弹窗:

技术分享

JSP/Servlet Web 学习笔记 DayFour —— 实现一个简单的JSP/Servlet交互

标签:dmi   serer   rpd   ddt   cdt   md5   zll   ctr   mapr   

原文地址:http://www.cnblogs.com/rekent/p/6947354.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!