您的位置:首页 > Web前端 > JavaScript

第一个JSP

2016-05-24 21:57 381 查看
还是大一暑假学的Java,知识掌握了第一阶段的Java基础知识, 后面想自己进阶第二阶段,学习JavaWeb,一直拖到现在直接从Java第二阶段写了~~~

希望指正;

这次主要还是第一个Hello World的开始了~~

我用的IDE是Java EE IDE Mars的

在eclipse中建的文件夹目录如下:



下面是index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Hello World
</body>
</html>


MyWeb.java

package com.cn.taobao.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

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

public class MyWeb extends HttpServlet {
private String message;
private char termName;
Scanner sc = new Scanner(System.in);
public void init(){
//执行必须的初始化
message = "Hello world。。。。";
System.out.println("Please enter the date ");
int date = sc.nextInt();
System.out.println(date);
}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{
//设置响应内容类型
response.setContentType("text/html");

//实际的逻辑是在这里
PrintWriter out = response.getWriter();
out.println("<h1   style='color:red' onClick='alert('zbang'); >"  +  message + "</h1>");

}
public void destroy(){
//do nothing..
}
}


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>Lesson01</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>
<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>MyWeb</servlet-name>
<servlet-class>com.cn.taobao.servlet.MyWeb</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CheckAccount</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>


运行效果如下(虽然很丑~~~~):



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: