java 第一个网页程序

java 第一个网页程序_第1张图片

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.

  *
  * 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("hello world");
 }

}

 

2.web.XML

文件内容:


 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">
 
    This is the description of my J2EE component
    This is the display name of my J2EE component
    Helloworld
    test.Helloworld
   
 

 

 

 
    Helloworld
    /Helloworld
 

 
    index1.jsp
 


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"));
}
%>



 
   
   
    My JSP 'index.jsp' starting page
 
 
    
 
 
 
 
 
 
 
    This is my JSP page14122scavsd1.

    This is my JSP page14122scavsd1.

   这个JSP 页面的功能是求两个整数的和

  
  



这个JSP 页面的功能是求两个整数的和:

请输入第一个数:

请输入第二个数:

这两个数的和为:<%=(first+second) %>



 

4.打开servers

java 第一个网页程序_第2张图片

5.输入网址:

java 第一个网页程序_第3张图片

6.输入数,然后按钮

java 第一个网页程序_第4张图片

 



 

 

你可能感兴趣的:(java 第一个网页程序)