java 第一个网页程序

来源:互联网 发布:linux中passwd命令 编辑:程序博客网 时间:2024/04/30 13:37

1.在src下建立一个包test,然后建立一个Helloworld.java程序,程序文件内容

package test;

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

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

public class Helloworld extends HttpServlet {

 /**
  * The doGet method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to get.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  this.doPost(request, response);
  
  
 }
 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  // TODO Auto-generated method stub
//  super.doPost(req, resp);
  resp.setContentType("text/html");
  PrintWriter out = resp.getWriter();
  out.println("<b>hello world</b>");
 }

}

 

2.web.XML

文件内容:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <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>Helloworld</servlet-name>
    <servlet-class>test.Helloworld</servlet-class>
   
  </servlet>
 

 

  <servlet-mapping>
    <servlet-name>Helloworld</servlet-name>
    <url-pattern>/Helloworld</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index1.jsp</welcome-file>
  </welcome-file-list>
</web-app>

3.建立index1.jsp文件

文件内容:

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

<%
int first = 0;
int second = 0;
if(request.getParameter("first")!=null&&request.getParameter("first").length()>0)
{
first = Integer.parseInt(request.getParameter("first"));
}
if(request.getParameter("second")!=null&&request.getParameter("second").length()>0)
{
second = Integer.parseInt(request.getParameter("second"));
}
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'index.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">
 -->
 <script type="text/javascript">
function check()
{
if(this.document.forms[0].first.value.length==0)
alert("请输入第一个整数!");
else if(this.document.forms[0].second.value.length==0)
alert("请输入第二个整数!");
else if (isNaN(this.document.forms[0].first.value))
alert("输入的第一个数字必须是整型数据");
else if (isNaN(this.document.forms[0].second.value))
alert("输入的第二个数字必须是整型数据");
else
this.document.forms[0].submit();
}
</script>
  </head>
 
  <body>
    This is my JSP page14122scavsd1. <br>
    This is my JSP page14122scavsd1. <br>
   这个JSP 页面的功能是求两个整数的和<br>
  
   <form action="Helloworld" method="post">
<font size="2">
这个JSP 页面的功能是求两个整数的和:<br>
请输入第一个数:<input type="text" name="first"/><br>
请输入第二个数:<input type="text" name="second"/><br>
这两个数的和为:<%=(first+second) %><br></font>
<button type="submit" value="求和" ></button><br>
</form>

  </body>

</html>

4.打开servers

5.输入网址:

6.输入数,然后按钮

 



 

 

0 0
原创粉丝点击