What have done in the last 2 days? (2010-10-05)(Part 2)
Another work implements a servlet to handle user's request. Recently I change my phone No. for some reason. I want to make a check that if a person is my classmates, he could get my new phone No. The validation is very simple i.e. the last name of our head teacher. Because java didn't support Chinese natively and I don't want to change the charset of the jsp file, I just use Pinyin to check the string.
In the jsp file there is a form:
<form action="/sign" method="post">
<div>
你是我的<select name="school" id="idschool">
<option value ="primary">小学同学</option>
<option value ="junior">初中同学</option>
<option value="high">高中同学</option>
<option value="college">大学同学</option>
</select> ?
</div>
<div>请输入三年级时班主任的姓氏拼音:<br>
<input name="headteacher" type="text" /></div>
<div><input type="submit" value="Get it!" /></div>
</form>
to make a the form submited correctly, in the war/WEB-INF/web.xml file should add:
<servlet>
<servlet-name>sign</servlet-name>
<servlet-class>guestbook.SignGuestbookServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sign</servlet-name>
<url-pattern>/sign</url-pattern>
</servlet-mapping>
In the package guestbook, SignGuestbookServlet.java handle the requset and make response.
public class SignGuestbookServlet extends HttpServlet{
private static final Logger log = Logger.getLogger(SignGuestbookServlet.class.getName());
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
req.setCharacterEncoding("utf-8");
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
resp.setContentType("text/html;charset='utf-8' ");
String school = req.getParameter("school");
String headteacher = new String(req.getParameter("headteacher").getBytes("ISO-8859-1"),"utf-8");
String content = req.getParameter("content");
//log.info(school);
log.info(headteacher);
String htmlcontent = "<html><body align='center'><br><br><br><br>" +
" <strong>My No.<strong><br>" +
" <p><a href='/'>back</a></p><body></html>";
if(school!=null&&headteacher!=null){
if(school.equals("primary")&&headteacher.equals("XX")){
PrintWriter out = resp.getWriter();
out.println(htmlcontent);
Then all the work is done. I have forgotten most part of servlet and jsp which I learned 4 years ago. The application is hosted on Google app engine. I have learned this before for using gaeproxy. This time I want to do a small experiment by myself. Sina also supply a cloud platform name sina app, but it is not open to everybody now. The import thing is that Sina require fees but Google app engine is free for 500M each month. But there is a risk to use Google’s service, we don't know when we will not be able to access to Google through the GFW. I download eclipse Helios JavaEE edition, the simple java edition didn’t work for jsp. Then follow the google app engine document’s instruction to add Google eclipse plugin and Google app engine java sdk. I studied the “Guestbook” tutorial of the document to learn the development on google app engine. It take me half a day to learn the basic development on GAE, mainly because I make a dumb mistake (I use the simple java edition not support jsp). I have use Google app engine before so I have an account to upload my project. It is hard to select a good name for your app as a lot of name is not available. At last I select urdoomtime.appspot.com. Urdoomtime means your doom time because my object is to make a time left count down tool. The inspiration is from the movie “2012”, so I make 2012-12-21 as the default end time.
I just did a simple test when I study GAE. It's a good opportunity to recover my javascript and jsp&servlet knowledge. I also spend two hour to study JQuery lib. It is a cool lib that can help you construct complex javascript application easily. Google app engine is really a good platform to deploy your application by Google’s infrastructure.
The computer and network in the college of information engineer masters' study room are really rubbish. The poor environment makes you waste a lot time when you start an eclipse and search some material on Internet. However, I have to bear it for another year.
Appendix
How to download a project from github?
1. Download msysgit : http://code.google.com/p/msysgit/ and install it
2. Create a directory where you want to put the project. E:\clock\
3. In the directory select “Git Bash” on the right click menu. Then a command window show up.
4. Use git clone to download the project: “git clone git://github.com/simonbaird/CoolClock.git”
Google app engine homepage?
http://code.google.com/appengine/
my google app engine test page?
Http:// urdoomtime.appspot.com
JQuery homepage?
Jquery.com
A good website to learn jQuery?
http://www.k99k.com/jQuery_getting_started.html
CoolClock homepage?
http://randomibis.com/coolclock/
本文使用Blog_Backup未注册版本导出,请到soft.pt42.com注册。