首页 > 代码库 > JSP/Servlet Web 学习笔记 DayFour —— 实现一个简单的JSP/Servlet交互
JSP/Servlet Web 学习笔记 DayFour —— 实现一个简单的JSP/Servlet交互
小实例说明:
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="http://www.mamicode.com/提交"> <input type="reset" value="http://www.mamicode.com/清空"> </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交互
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。