servlet

刚刚学了Java Servlet, 中间遇到了一些小问题, 最然都是最基础的,都是还是想写下来~~~

首先先介绍一下Java Servlet:

360百科http://baike.so.com/doc/2354665-2490021.html

下面是在Java EE中创建的关于Servlet相关的基础练习:

文件目录:

下面是代码:

MeWeb.java

package com.taobao.servlet;

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

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 MeWeb extends HttpServlet {
		private String message;
		public void init() {
			// 执行必需的初始化
			message = "Hello World!";
		}

		public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
//			// 设置响应内容类型
//			response.setContentType("text/html; charset=UTF-8; pageEncoding=UTF-8");
//			response.setCharacterEncoding("UTF-8");
//			// 实际的逻辑是在这里
//			String userName = request.getParameter("userName");
//			System.out.println(userName);
//			PrintWriter out = response.getWriter();
//			out.println("<h1 style='color:red' onClick='alert('你很棒');'>" + message + userName + "</h1>");
		
			RequestDispatcher d =request.getRequestDispatcher("content.jsp");
			request.setAttribute("username","小明");
			d.forward(request, response);
		
		}
		

		public void destroy() {
			// 什么也不做
		}
	}

 MeWeb.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>
<body>
Hello World........
</body>

<script type="text/javascript">
function get(){   //方法体
	alert(" What did you do");	
}
  get();
</script>
<h1 style="color:red"onClick="get();"> Click On Me</h1>

</html>

 content.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>
<body>
 <!-- <% %> -->
 <!--  --> 
 <%= request.getAttribute("username")%>你好!
</body>
</html>

 index.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>
<body>
Hello World~~~
<script type="text/javascript">
function get(){   //方法体
	alert(" What did you do");	
}
  get();
</script>
<h1 style="color:red"onClick="get();"> Click On Me</h1>
</body>
</html>

 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Lessonone</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>MeWeb</servlet-name>
    <servlet-class>com.taobao.servlet.MeWeb</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>MeWeb</servlet-name>
    <url-pattern>/meweb</url-pattern>
  </servlet-mapping>
 
</web-app>

 运行效果如下:

都是最最基础的, 还是想写出来。。希望指正~~~

转载于:https://www.cnblogs.com/haixiaomei/p/a525.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值