java HW Anketa

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java HW Anketa相关的知识,希望对你有一定的参考价值。

<?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"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">


	<servlet>
		<servlet-name>Anketa</servlet-name>
		<servlet-class>ua.kiev.prog.AnketaServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>Anketa</servlet-name>
		<url-pattern>/anketa</url-pattern>
	</servlet-mapping>
	
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>
</web-app>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- The HTML 4.01 Transitional DOCTYPE declaration-->
<!-- above set at the top of the file will set     -->
<!-- the browser's rendering engine into           -->
<!-- "Quirks Mode". Replacing this declaration     -->
<!-- with a "Standards Mode" doctype is supported, -->
<!-- but may lead to some differences in layout.   -->

<html>
  <head>
    <title>Home work: anketa</title>
  </head>
  <body>
    <form action="/anketa" method="POST">
        First name: <input type="text" name="name"><br>
        Last name: <input type="text" name="surname"><br>
        Age: <input type="text" name="age"><br>
        <br>
        <b>Now answer few questions</b><br>
        <p><b> 1.How many years you've been coding?</b><Br>
            <input type="radio" name="coding" value="1"> 1 year or less<Br>
            <input type="radio" name="coding" value="2"> From 1 to 3 years<Br>
            <input type="radio" name="coding" value="3"> More than 3 years<Br>
        </p>
        <p><b> 2. Do you professional programmer?</b><Br>
            <input type="radio" name="prof" value="yes"> Yes <Br>
            <input type="radio" name="prof" value="no"> No <Br>
        </p>
        <input type="submit"></input>
    </form>
  </body>
</html>
package ua.kiev.prog;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.*;

@SuppressWarnings("serial")
public class AnketaServlet extends HttpServlet {
	
	static final String TEMPLATE = "<html>" +
			"<head><title>Home work: statistic</title></head>" +
			"<body><h1>Statistic</h1></body>" +
            "<p><b>We've got following answers:</b></p>" +
            "<p><b>1. How many years you've been coding? </b><br>" +
            " '1 year or less' - %s answers <br>" +
            " 'from 1 to 3 years' - %s answers <br>" +
            " 'more than 3 years' - %s answers <br></p>" +
            "<p><b>2. Are you professional programmer? </b><br>" +
            " 'Yes' - %s answers <br>" +
            " 'No' - %s answers <br></p>" +
            " Last visitor is - %s %s, %s years old. <br></p>" +
            "<p><a href='http://localhost:8080/'> Back to main page</a></p>" +
            "</html>";

	private int cod1 = 0;
    private int cod2 = 0;
    private int cod3 = 0;
    private int yes = 0;
    private int no = 0;
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String name = req.getParameter("name");
		String surname = req.getParameter("surname");
        String age = req.getParameter("age");
        String coding = req.getParameter("coding");
        String prof = req.getParameter("prof");

        if(coding.equals("1")) {cod1++;}
        if(coding.equals("2")) {cod2++;}
        if(coding.equals("3")) {cod3++;}
        if(prof.equals("yes")) {yes++;}
        if(prof.equals("no")) {no++;}

        resp.getWriter().println(String.format(TEMPLATE, cod1, cod2, cod3, yes, no, name, surname, age));
	}
}

以上是关于java HW Anketa的主要内容,如果未能解决你的问题,请参考以下文章

java HW 10.4

java HW 7.2

java HW 7.3

java HW 7.1

java HW 6.2

java HW 5.3-5.4