servlet2:重定向,请求转发,servlet获取参数等
package cam. xuedao. servlet;
import java. io. IOException;
import java. util. Enumeration;
import java. util. Map;
import java. util. Map. Entry;
import javax. servlet. ServletException;
import javax. servlet. http. HttpServlet;
import javax. servlet. http. HttpServletRequest;
import javax. servlet. http. HttpServletResponse;
import cam. xuedao. pojo. User;
public class DeptServlet extends HttpServlet {
private static final long serialVersionUID = 1 L;
protected void doGet1 ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request. getParameter ( "name" ) ;
Enumeration< String> names = request. getParameterNames ( ) ;
while ( names. hasMoreElements ( ) ) {
String name = names. nextElement ( ) ;
System. out. println ( name) ;
}
System. out. println ( request. getParameterValues ( "like" ) ) ;
Map< String, String[ ] > map = request. getParameterMap ( ) ;
for ( Entry< String, String[ ] > entry : map. entrySet ( ) ) {
String name = entry. getKey ( ) ;
String[ ] values = entry. getValue ( ) ;
System. out. println ( name + ":" ) ;
for ( String str : values) {
System. out. println ( str + "" ) ;
}
}
}
@Override
protected void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request. getParameter ( "username" ) ;
String password = request. getParameter ( "password" ) ;
String path0 = request. getContextPath ( ) ;
String path1 = request. getServletPath ( ) ;
String path2 = request. getServletContext ( ) . getRealPath ( "aa" ) ;
if ( "123" . equals ( username) && "123" . equals ( password) ) {
request. getRequestDispatcher ( "/error.html" ) . forward ( request, response) ;
} else {
response. sendRedirect ( "login.html" ) ;
}
}
protected void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet ( request, response) ;
}
}
< form action = " dept" method = " post" >
用户名:<input type= "text" name = "username">< br>
密 码:<input type = "password" name = "password">< br>
<input type = "submit">
</ form>