从com_.net_java三个方面讲述ae_JavaWeb 程序设计 习题参考答案(第1-6章)
时间: 2023-06-23 16:08:50 浏览: 120
com、net_、java_ 是Java中的三个常用的包(package),它们用于对类进行组织和分类。而 ae_JavaWeb程序设计是一本JavaWeb方面的教材,下面将从com_、net_java三个方面讲述该教材第1-6章的习题参考答案。
1. com_方面
第1章:JavaWeb基础
习题1-1 参考答案:
```java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
```
习题1-2 参考答案:
```html
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
```
第2章:Servlet技术
习题2-1 参考答案:
```java
@WebServlet("/hello")
public class HelloWorldServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Hello World</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello World</h1>");
out.println("</body>");
out.println("</html>");
}
}
```
习题2-2 参考答案:
```java
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name = req.getParameter("name");
String age = req.getParameter("age");
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>MyServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>姓名:" + name + "</h1>");
out.println("<h1>年龄:" + age + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
```
2. net_方面
第3章:JSP技术
习题3-1 参考答案:
```jsp
<%@ page language="java" contentType="text/html;charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
```
习题3-2 参考答案:
```jsp
<%@ page language="java" contentType="text/html;charset=UTF-8" %>
<html>
<head>
<title>MyJSP</title>
</head>
<body>
<h1>姓名:<%= request.getParameter("name") %></h1>
<h1>年龄:<%= request.getParameter("age") %></h1>
</body>
</html>
```
第4章:JSTL和EL技术
习题4-1 参考答案:
```jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<c:set var="name" value="World" />
<h1>Hello ${name}</h1>
</body>
</html>
```
习题4-2 参考答案:
```jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>MyJSP</title>
</head>
<body>
<c:set var="name" value="${param.name}" />
<c:set var="age" value="${param.age}" />
<h1>姓名:<c:out value="${name}" /></h1>
<h1>年龄:<c:out value="${age}" /></h1>
</body>
</html>
```
3. java_方面
第5章:JavaBean技术
习题5-1 参考答案:
```java
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
```
习题5-2 参考答案:
```jsp
<jsp:useBean id="person" class="com.example.Person" scope="request" />
<jsp:setProperty name="person" property="name" value="<%= request.getParameter("name") %>" />
<jsp:setProperty name="person" property="age" value="<%= request.getParameter("age") %>" />
<html>
<head>
<title>MyJSP</title>
</head>
<body>
<h1>姓名:<jsp:getProperty name="person" property="name" /></h1>
<h1>年龄:<jsp:getProperty name="person" property="age" /></h1>
</body>
</html>
```
第6章:过滤器技术
习题6-1 参考答案:
```java
@WebFilter(filterName = "EncodingFilter", urlPatterns = "/*")
public class EncodingFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
}
```
习题6-2 参考答案:
```java
@WebFilter(filterName = "LoginFilter", urlPatterns = "/login.jsp")
public class LoginFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest)request;
HttpServletResponse resp = (HttpServletResponse)response;
String username = req.getParameter("username");
String password = req.getParameter("password");
if (username != null && password != null && username.equals("admin") && password.equals("admin")) {
chain.doFilter(request, response);
} else {
resp.sendRedirect(req.getContextPath() + "/error.jsp");
}
}
@Override
public void destroy() {
}
}
```
阅读全文