ServletContext题目

建立/java文件:

src右键→new→servlet→

        javapackage:servlet

        class name:DisplayServlet(本题目建立了DisplayServlet

        next→

        选中URL mappings:里面的/DisplayServlet,选中然后点右边的edit...,去掉后面的Servlet,把前面的Display换成display(大写换成小写)

        取消勾选constructors from superclass

        勾选inherited abstract methods

        勾选doget,dopost

        finish→

可以把.java里面多余的删掉


建立.jsp文件:

.jsp文件建立在webroot下!(不要建立在webroot的web-int下,web-int文件夹下的内容机密性很高,网页无法访问)  


web.xml文件在webroot的web-int下!

DisplayServlet.java

package servlet;

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

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


@WebServlet(
		urlPatterns = { "/display" }, 
		initParams = { 
				@WebInitParam(name = "info", value = "123456")
		})
public class DisplayServlet extends HttpServlet {
	//private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		
		response.setContentType("text/html");
		PrintWriter out=response.getWriter();				
		out.print("<html>");
		out.print("<head>");
		out.print("</head>");
		out.print("<body>");		
		out.print("display<br>");
		ServletConfig config=this.getServletConfig();
		
		String value=config.getInitParameter("info");		
		out.print("info----"+value+"<br>");
		
		ServletContext application=this.getServletContext();
		String value2=application.getInitParameter("message");
		out.print("message----"+value+"<br>");
				
		out.print("</body>");
		out.print("</html>");		
	}

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

}

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_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>demo1028</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>
  
  <context-param>
  	<param-name>message</param-name>
  	<param-value>QTC.EDU.CN</param-value>
  </context-param>
  
</web-app>

index.jsp

<%@ page language="java" contentType="text/html; charset=gbk"
    pageEncoding="gbk"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<font size="5">
	所谓的JSP页面,就是在HTML代码中嵌入JAVA代码。这是HTML代码<br>
	ISO:国际标准化组织<br>
	<% 
	int a=10;
	int sum=100;
	%>写在这里的是Java代码
	a=<%=a %><br>
	sum=<%=sum %><br>
	
	<%
	//ServletContext application=this.getServletContext();
	String value2=this.getServletContext().getInitParameter("message");
	%>
	
	</font>

</body>
</html>

整体文件放置:

配置(忘记可查看Javaweb题目:输入圆的半径,求面积_abandon_i的博客-CSDN博客在文章偏下位置)

internal web browser中输入http://127.0.0.1:8080//demo1028/display

输入http://127.0.0.1:8080//demo1028/index.jsp

成功运行

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值