2-13 自由编程

2-13 自由编程

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

<style>

</style>

</head>

<body>


<div style="line-height: 37px; font-size: 23px">

<p>加法计算器</p>

<form action="http://localhost:8080/Sum/sum">

<label>加数1:</label> 

<input type="text" name="a1" value=""> </br> 

<label>加数2:</label>

<input type="text" name="a2" value=""> </br> 

<input type="submit" value="计算"> </br>

</form>

</div>


</body>

</html>



<?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_3_1.xsd" id="WebApp_ID" version="3.1">

  <display-name>Sum</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>sum</servlet-name>

  <servlet-class>com.immoc.Servlet.SumCumputer</servlet-class>

  </servlet>

  <servlet-mapping>

  <servlet-name>sum</servlet-name>

  <url-pattern>/sum</url-pattern>

  </servlet-mapping>

</web-app>



package com.immoc.Servlet;


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 SumCumputer extends HttpServlet{


@Override

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

//super.service(arg0, arg1);

request.setCharacterEncoding("utf-8");

response.setCharacterEncoding("utf8");

response.setContentType("text/html;charset=utf-8");

String a1=request.getParameter("a1");

String a2=request.getParameter("a2");

int sum=Integer.parseInt(a1)+Integer.parseInt(a2);

PrintWriter out=response.getWriter();

out.println("<p>加法计算器:</br>运算结果为:" + sum + "</p></br>");

}


}


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕小班 2019-12-23 11:38:14

同学你好,代码完成的没有问题!继续努力!

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星

相似问题

登录后可查看更多问答,登录/注册

请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师