javaEE JSP+Servlet+JavaBean 5种开发模式

5种开发模式的总结

1、利用单纯的JSP页面开发模式

设计Web程序,计算1+2+3+….+100的和值,并在网页上显示结果。

文件结构


index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
	</head>

	<body>
	<%
		int i;
		int sum=0;
  		for(i=1;i<=100;i++)
  		sum+=i;
   %>
		<p>
			这是一单纯的jsp页面编程示例
		</p>
		<p>
			1到100的和为:
	<%=sum %>
		</p>
	</body>
</html>


2、利用单纯的JSP页面开发模式

设计Web程序,计算任意两个整数之间累加和值,并在网页上显示结果。

文件结构


index.jsp输入数据后将数据传入show.jsp中显示结果。

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>

	</head>

	<body>
	按下列格式要求输入两个整数:
		<form method="post" action="show.jsp">
			开始数据:
			<input name="data1" type="text">
			<br>
			结束数据:
			<input name="data2" type="text">
			<br>
			<input value="提交" type="submit">
			<br>
		</form>
	</body>
</html>
show.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>

	</head>

	<body>
	按下列格式要求输入两个整数:
		<form method="post" action="show.jsp">
			开始数据:
			<input name="data1" type="text">
			<br>
			结束数据:
			<input name="data2" type="text">
			<br>
			<input value="提交" type="submit">
			<br>
		</form>
	</body>
</html>


3、JSP+JavaBean开发模式 

设计Web程序,计算任意两个整数之间累加和值,并在网页上显示结果.

index.jsp输入数据后将数据传入show.jsp中,然后再调用JavaBean中的方法运算并显示结果。

文件结构


dataDeal.java 自己写的javaBean

package javaBean;

public class dataDeal {
	private int data1, data2;

	public int getData1() {
		return data1;
	}

	public void setData1(int data1) {
		this.data1 = data1;
	}

	public int getData2() {
		return data2;
	}

	public void setData2(int data2) {
		this.data2 = data2;
	}

	public int show() {
		int sum = 0;
		for (; data1 <= data2; data1++)
			sum += data1;
		return sum;
	}
}
index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
   
  </head>
  
  <body>
    按下列格式要求输入两个整数:
		<form method="post" action="show.jsp">
			开始数据:
			<input name="data1" type="text">
			<br>
			结束数据:
			<input name="data2" type="text">
			<br>
			<input value="提交" type="submit">
			<br>
		</form>
  </body>
</html>
show.jsp

<%@ page language="java" import="java.util.*" import="javaBean.dataDeal"  pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
	</head>

	<body>
		<%
			String d1 = request.getParameter("data1");
			String d2 = request.getParameter("data2");
			int data1 = Integer.parseInt(d1);
			int data2 = Integer.parseInt(d2);
			dataDeal d=new dataDeal();
			d.setData1(data1);
			d.setData2(data2);
		%>
		<p>
		<%=d1 + "加到" + d2 + "的和值是" +d.show() %>
		</p>
		<p>
			<%="现在的时间是:" + new java.util.Date()%>
		<p>
	</body>
</html>

4、JSP+Servlet开发模式 

设计Web程序,计算任意两个整数之间累加和值,并在网页上显示结果.

文件结构


index.jsp输入数据后将数据传入servlet中,然后servle把数据]共享给show.jsp并完成运算显示结果。

这需要修改web.xml文件

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>

	</head>

	<body>
		按下列格式要求输入两个整数:
		<form method="post" action="runner">
			开始数据:
			<input name="data1" type="text">
			<br>
			结束数据:
			<input name="data2" type="text">
			<br>
			<input value="提交" type="submit">
			<br>
		</form>
	</body>
</html>
show.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

</head>

<body>
	<%
 		String sum = (String) request.getAttribute("sum");
		String d1 = (String) request.getAttribute("data1");
		String d2 = (String) request.getAttribute("data2");
	%>
	<p>
		<%=d1 + "加到" + d2 + "的和值是" +sum%>
	</p>
	<p>
		<%="现在的时间是:" + new java.util.Date()%>
	</p>

</body>
</html>
servlet1.java完成跳转的

package servletConnection;

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 servlet1 extends HttpServlet {

	public servlet1() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

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

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String d1 = request.getParameter("data1");
		String d2 = request.getParameter("data2");
		int data1 = Integer.parseInt(d1);
		int data2 = Integer.parseInt(d2);
		int sum=0;
		for(;data1<=data2;data1++){
			sum+=data1;
		}
		String s=""+sum;
		request.setAttribute("data1", d1);
		request.setAttribute("data2", d2);
		request.setAttribute("sum", s);
		request.getRequestDispatcher("show.jsp").forward(request, response);
	}

	public void init() throws ServletException {
		// Put your code here
	}

}
web.xml url-pattern需要与index.jsp中form的action一致

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	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_2_5.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>servlet1</servlet-name>
    <servlet-class>servletConnection.servlet1</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>servlet1</servlet-name>
    <url-pattern>/runner</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

5、JSP+Servlet+JavaBean开发 模式

设计Web程序,计算任意两个整数之间累加和值,并在网页上显示结果

将第3种和第4种模式整合到一起,首先index.jsp接受数据,然后servlet把数据共享给show.jsp,show.jsp再调用JavaBean计算并显示结果。

文件结构

javaBean类

package javaBean;

public class dataDeal {
	private int data1, data2;

	public int getData1() {
		return data1;
	}

	public void setData1(int data1) {
		this.data1 = data1;
	}

	public int getData2() {
		return data2;
	}

	public void setData2(int data2) {
		this.data2 = data2;
	}

	public int show() {
		int sum = 0;
		for (; data1 <= data2; data1++)
			sum += data1;
		return sum;
	}
}
index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
   
  </head>
  
  <body>
    按下列格式要求输入两个整数:
		<form method="post" action="runner">
			开始数据:
			<input name="data1" type="text">
			<br>
			结束数据:
			<input name="data2" type="text">
			<br>
			<input value="提交" type="submit">
			<br>
		</form>
  </body>
</html>
show.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

</head>

<body>
	<%
 		String sum = (String) request.getAttribute("sum");
		String d1 = (String) request.getAttribute("data1");
		String d2 = (String) request.getAttribute("data2");
	%>
	<p>
		<%=d1 + "加到" + d2 + "的和值是" +sum%>
	</p>
	<p>
		<%="现在的时间是:" + new java.util.Date()%>
	</p>

</body>
</html>
servlet

package servletConnection;

import java.io.IOException;
import java.io.PrintWriter;
import javaBean.dataDeal;

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

@SuppressWarnings({ "unused", "serial" })
public class servlet1 extends HttpServlet {

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

		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String d1 = request.getParameter("data1");
		String d2 = request.getParameter("data2");
		int data1 = Integer.parseInt(d1);
		int data2 = Integer.parseInt(d2);
		dataDeal d=new dataDeal();
		d.setData1(data1);
		d.setData2(data2);
		int sum=d.show();
		String s=""+sum;
		request.setAttribute("data1", d1);
		request.setAttribute("data2", d2);
		request.setAttribute("sum", s);
		request.getRequestDispatcher("show.jsp").forward(request, response);
	}

	public void init() throws ServletException {
		// Put your code here
	}

}
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	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_2_5.xsd">
  <display-name></display-name>
  <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>servlet1</servlet-name>
    <servlet-class>servletConnection.servlet1</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>servlet1</servlet-name>
    <url-pattern>/runner</url-pattern>
  </servlet-mapping>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值