目录
题目:实现一个计算教师工资的项目
项目目录结构
页面一:Employee.java
package entitylogic;
public abstract class Employee {
String title;
float wage;
String name;
abstract void calculateWage();
public Employee(String name,String title)
{
super();
this.name=name;
this.title=title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public float getWage() {
return wage;
}
public void setWage(float wage) {
this.wage = wage;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
页面二:FulltimeTeacher.java
package entitylogic;
public class FulltimeTeacher extends Employee{
float extrahours;
float basicwage;
public FulltimeTeacher(String name,String title)
{
super(name, title);
}
public float getExtrahours() {
return extrahours;
}
public void setExtrahours(float extrahours) {
this.extrahours = extrahours;
}
public float getBasicwage() {
return basicwage;
}
public void setBasicwage(float basicwage) {
this.basicwage = basicwage;
}
public void calculateWage() {
if(this.title.equals("副教授")) {
this.basicwage=4000;
wage=this.basicwage+this.extrahours*80;
}else if(this.title.equals("教授")){
this.basicwage=6000;
wage=this.basicwage+this.extrahours*100;
}
}
}
页面三:Cal.java
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import entitylogic.FulltimeTeacher;
/**
* Servlet implementation class Cal
*/
@WebServlet("/Cal")
public class Cal extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Cal() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
try {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset= utf-8");
String name = request.getParameter("employeeName");
String title = request.getParameter("employeeTitle");
Float extraClasshour = Float.parseFloat(request
.getParameter("employeeExtraClasshour"));
FulltimeTeacher pt2 = new FulltimeTeacher(name, title);
pt2.setExtrahours(extraClasshour);
pt2.calculateWage();
Float wage = pt2.getWage();
ServletContext context = getServletContext();
RequestDispatcher rd = context
.getRequestDispatcher("/jsp/show.jsp?wage=" + wage);
rd.forward(request, response);
} catch (Exception ex) {
PrintWriter out = response.getWriter();
out.println("<html><head><title>");
out.println("返回重填页面");
out.println("</title></head><body>");
out.println("出错了");
out.println(ex.getMessage());
String url = this.getServletContext().getContextPath()+"/jsp/input.jsp";
out.println("<form action="+url+">");
out.println("<input type=submit value=返回重填>");
out.println("</form>");
out.println("</body></html>");
out.close();
}
}
}
页面四:input.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%String url=this.getServletContext().getContextPath()+"/Cal"; %>
<form method=post action =<%=url %>>
<div style="text-align:center ">本院全职教师工资计算</div>
请输入:<br>
姓名: <input name ="employeeName" type ="text" ><br>
职称:<input name ="employeeTitle" type ="radio" value="副教授" checked ="checked">
副教授<input name ="employeeTitle" type ="radio" value="教授">教授<br>
本月超额课时为:<input name ="employeeExtraClasshour" type ="text"><br>
<input name ="CalculateWage" type="submit" value = "计算">
<input name ="reset" type="reset" value = "重填">
<br></br>
</form>
</body>
</html>
页面五:show.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String name = request.getParameter("employeeName");
float wage = Float.parseFloat(request.getParameter("wage"));
%>
以下是您提交的信息,请确认:
<br>
<%=name%><br> 本月工资为:
<%=wage%>
</body>
</html>