JavaWeb基础 jsp+servlet分工合作的简单示例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaWeb基础 jsp+servlet分工合作的简单示例相关的知识,希望对你有一定的参考价值。

礼悟:
     好好学习合思考,尊师重道存感恩。叶见寻根三返一,江河湖海同一体。
          虚怀若谷良心主,愿行无悔给最苦。读书锻炼强身心,诚劝且行且珍惜。




       javaEE:7           
       javaSE:1.8
          JSTL:1.2.2  
      server:tomcat 8.5
    browser:Chrome/Firefox
             os:windows7 x64
            ide:MyEclipse 2017

 

项目结构

 技术分享图片

 


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Day11</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
  	<servlet-name>addServlet</servlet-name>
  	<servlet-class>com.jizuiku.servlet.addServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>addServlet</servlet-name>
  	<url-pattern>/addServlet</url-pattern>
  </servlet-mapping>
  
</web-app>

 

addServlet.java

package com.jizuiku.servlet;

import java.io.IOException;

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

/**
 * 
 * 
 * @author 博客园-给最苦 
 * @version V2017.11.20
 */
public class addServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	}
	
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		// 获取参数
		String numStr = request.getParameter("num");
		
		// 转换成十六进制 
		String numHexStr  = Integer.toHexString(Integer.parseInt(numStr));
		
		// 把结果保存到request域中
		request.setAttribute("result", numHexStr);
		
		// 转换到result.jsp
		request.getRequestDispatcher("/addDemo/result.jsp").forward(request, response);;
		
	}
}

 

form.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP ‘form.jsp‘ starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
	<form action="/Day11/addServlet" method="post">
		整数:<input type="text" name="num"/><br />
		<input type="submit" value="提交">
	</form>    
  </body>
</html>

 

result.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP ‘result.jsp‘ starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

</head>

<body>
	hello,i am result.jsp<br />
	<!-- 显示结果 -->
	<%
		String arg = request.getParameter("num");
		String result = (String)request.getAttribute("result");
	%>
	<%="输入的参数是:" + arg%>
	<br />
	<%="运算的结果是:" + result%>
</body>
</html>

 

浏览器测试

技术分享图片

 

 

技术分享图片

 

 


学习资源:itcast和itheima视频库。如果您有公开的资源,可以分享给我的话,用您的资源学习也可以。
博文是观看视频后,融入思考写成的。博文好,是老师讲得好。博文坏,是 给最苦 没认真。















以上是关于JavaWeb基础 jsp+servlet分工合作的简单示例的主要内容,如果未能解决你的问题,请参考以下文章

JavaWeb-----JSP基础知识①

JavaWeb之JSP

JavaWeb数据库应用开发基础

JavaWeb基础教学servlet+jsp+druid实现增删改查

javaweb核心之页面技术

JavaWeb基础入门讲解