Servlet练习
Posted 曦~妍
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Servlet练习相关的知识,希望对你有一定的参考价值。
编写一个Servlet,注册登录成功后,讲表单中的内容输出到页面当中
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <form action="ser" method="post"> 11 用户名<input type="text" name="username"> 12 <br><br> 13 密码 <input type="password" name="password"> 14 <br><br> 15 <input type="submit" value="登录"> 16 </form> 17 </body> 18 </html>
1 package com.servlet.web; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 import javax.servlet.http.HttpSession; 9 import javax.websocket.Session; 10 11 12 public class Servlet1 extends HttpServlet { 13 private static final long serialVersionUID = 1L; 14 15 public Servlet1() { 16 super(); 17 18 } 19 20 21 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 22 response.setContentType("text/html"); 23 response.setCharacterEncoding("UTF-8"); 24 String un=new String(request.getParameter("username").getBytes("ISO-8859-1"),"utf-8"); 25 String pw=new String(request.getParameter("password").getBytes("ISO-8859-1"),"UTF-8"); 26 27 if(un!=null&&pw!=null) 28 { 29 if(!un.equals("")) 30 { 31 if(un.equals("妍希")&&pw.equals("14567")) 32 { 33 response.getWriter().write(un+" 欢 迎 你 来 做 淄 博 游 玩!"+"<br>"); 34 } 35 else 36 { 37 response.getWriter().write("输入的账号和密码有误"); 38 } 39 } 40 else 41 { 42 response.getWriter().write("账号不能为空"); 43 } 44 } 45 else 46 { 47 response.getWriter().write("请正确访问"); 48 } 49 response.getWriter().append("Served at: ").append(request.getContextPath()); 50 } 51 52 53 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 54 55 doGet(request, response); 56 } 57 58 }
以上是关于Servlet练习的主要内容,如果未能解决你的问题,请参考以下文章
Python练习册 第 0013 题: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-),(http://tieba.baidu.com/p/2166231880)(代码片段