Servlet入门---张国亮---总结心德之作业

news/2024/7/10 6:12:49 标签: servlet, url, string, 作业, path, java

 

1. 写出获取ServletContext的两种方式

package com.hbsi.context;

import java.io.IOException;

import javax.servlet.ServletContext;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class Context1 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

//获取ServletContext方法1

ServletContext context1 = this.getServletConfig().getServletContext();

//获取ServletContext方法2

ServletContext context2 = this.getServletContext();

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);

}

}

2.使用ServletContext实现两个Servlet数据共享

package com.hbsi.context;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class Context2 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

//多个Servlet实现数据共享

String data="abcd";

this.getServletContext().setAttribute("data1", data);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);

}

}

3.设置ServletContext初始化参数,然后对其之。

package com.hbsi.context;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class Context3 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

//配合Context2使用

String value = (String) this.getServletContext().getAttribute("data1");

System.out.println(value);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);

}

}

4. 编写一个转发

package com.hbsi.context;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class Context4 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

System.out.println(this.getServletContext().getInitParameter("data"));

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);

}

}

5.通过ServletContext读取配置文件的内容。(使用两种方式)

package com.hbsi.context;

import java.io.IOException;

import javax.servlet.RequestDispatcher;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class Context5 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

//实现转发

this.getServletContext().setAttribute("username","zhangsan");

RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/index.jsp");

rd.forward(request, response);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);

}

}

6.通过一般的java类读取配置文件的内容。

package com.hbsi.context;

import java.io.IOException;

import javax.servlet.RequestDispatcher;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class Context5 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

//实现转发

this.getServletContext().setAttribute("username","zhangsan");

RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/index.jsp");

rd.forward(request, response);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);

}

}

package com.hbsi.context;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import java.util.Properties;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import com.hbsi.dao.StudentDao;

public class Context6 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

// 传统的方式错误了

/*

 * FileInputStream fis = new FileInputStream("src/db.properties");

 * 

 * Properties prop = new Properties(); prop.load(fis);

 * 

 * String driver = prop.getProperty("driver");

 * System.out.println(driver);

 */

StudentDao dao = new StudentDao();

dao.delete();

}

// 通过ServletContext读取配置文件 方法1

public void test1() throws IOException {

InputStream in = this.getServletContext().getResourceAsStream(

"/WEB-INF/classes/db.properties");

Properties prop = new Properties();

prop.load(in);

String driver = prop.getProperty("driver");

String url = prop.getProperty("url");

String username = prop.getProperty("username");

String password = prop.getProperty("password");

System.out.println(url);

}

// 通过ServletContext读取配置文件 方法2,获取了文件的绝对路径

public void test2() throws IOException {

String path = this.getServletContext().getRealPath(

"/WEB-INF/classes/db.properties");

int i = path.lastIndexOf("\\");

System.out.println(path.substring(i + 1));

FileInputStream fis = new FileInputStream(path);

Properties prop = new Properties();

prop.load(fis);

String driver = prop.getProperty("driver");

System.out.println(driver);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);

}

}


http://www.niftyadmin.cn/n/1860634.html

相关文章

JAVA程序员听到bug有何反应?

JAVA程序员的世界里,不止有代码,还有bug,bug,bug…当出现bug时,JAVA程序员们是如何反应的呢?

swift 组件化_Swift在好大夫APP医患两端的打怪升级

背景swift作为苹果的亲儿子,从2014年开始,在6年的发展过程中,终于在2019年3月份迎来了ABI(Application Binary Interface)的稳定,ABI的稳定意味着Binary接口稳定,也就是运行的时候只要是通过swift5或者以上的编译器编译…

中国IT排名百强公司

排序 单位名称 软件收入 1 华为技术有限公司 622360 2 中兴通讯股份有限公司 601331 3 海信集团有限公司 448641 4 UT斯达康通讯有限公司 386763 5 海尔集团公司 333664 6 神州数码(中国)有限公司…

python求均方根_python之MSE、MAE、RMSE的使用

我就废话不多说啦,直接上代码吧!target [1.5, 2.1, 3.3, -4.7, -2.3, 0.75]prediction [0.5, 1.5, 2.1, -2.2, 0.1, -0.5]error []for i in range(len(target)):error.append(target[i] - prediction[i])print("Errors: ", error)print(err…

学习JAVA的3大好处!

JAVA是目前最受欢迎的编程语言,更是IT领域的领航者,只要精通JAVA就能找到一份好工作。很多人都向往Java编程的广阔就业前景,却苦于不懂Java编程知识,一直在自学和参加培训中纠结,我们今天就来讨论下这个问题&#xff0…

办公室最酷的鼠标垫

少于200字,就不发布到网站首页啦 这是定制的。 转载于:https://www.cnblogs.com/balian/p/3626837.html

python call_Python __call__详解

可以调用的对象关于 __call__ 方法,不得不先提到一个概念,就是可调用对象(callable),我们平时自定义的函数、内置函数和类都属于可调用对象,但凡是可以把一对括号()应用到某个对象身上都可称之为可调用对象,判断对象是…

JAVA程序员跳槽硬气需要那些技术?

敢不敢不给涨薪就“挥一挥衣袖,不带走一个bug”?是不是提出要求后你的主管、经理立刻 同意,为了把你留住。然而,现实往往是... 技术不过硬,其他的都免谈。 小编给大家罗列一下近期最常用,最火的技术&#x…